|
|||
Previous < |
Contents ^
|
Next >
|
def
.
Method names should begin with a lowercase
letter.[You won't get an immediate error if you use an
uppercase letter, but when Ruby sees you calling the method, it
will first guess that it is a constant, not a method invocation, and
as a result it may parse the call incorrectly.] Methods that act as
queries are often named with a trailing ``?
'', such as
instance_of?
. Methods that are ``dangerous,'' or modify the
receiver, might be named with a trailing ``!
''. For instance,
String
provides both a chop
and a chop!
. The first one
returns a modified string; the second modifies the receiver in
place. ``?
'' and ``!
'' are the only weird characters allowed
as method name suffixes.
Now that we've specified a name for our new method, we may need to
declare some parameters.
These are simply a list of local variable
names in parentheses. Some sample method declarations are
def myNewMethod(arg1, arg2, arg3) # 3 arguments # Code for the method would go here end def myOtherNewMethod # No arguments # Code for the method would go here end |
def coolDude(arg1="Miles", arg2="Coltrane", arg3="Roach")
|
||
"#{arg1}, #{arg2}, #{arg3}."
|
||
end
|
||
|
||
coolDude
|
» |
"Miles, Coltrane, Roach."
|
coolDude("Bart")
|
» |
"Bart, Coltrane, Roach."
|
coolDude("Bart", "Elwood")
|
» |
"Bart, Elwood, Roach."
|
coolDude("Bart", "Elwood", "Linus")
|
» |
"Bart, Elwood, Linus."
|
return
expression.
def varargs(arg1, *rest)
|
||
"Got #{arg1} and #{rest.join(', ')}"
|
||
end
|
||
|
||
varargs("one")
|
» |
"Got one and "
|
varargs("one", "two")
|
» |
"Got one and two"
|
varargs "one", "two", "three"
|
» |
"Got one and two, three"
|
Array
, which is then assigned to that parameter.
yield
.
def takeBlock(p1) if block_given? yield(p1) else p1 end end |
takeBlock("no block")
|
» |
"no block"
|
takeBlock("no block") { |s| s.sub(/no /, '') }
|
» |
"block"
|
Proc
object, and that object is assigned to the parameter.
class TaxCalculator
|
||
def initialize(name, &block)
|
||
@name, @block = name, block
|
||
end
|
||
def getTax(amount)
|
||
"#@name on #{amount} = #{ @block.call(amount) }"
|
||
end
|
||
end
|
||
|
||
tc = TaxCalculator.new("Sales tax") { |amt| amt * 0.075 }
|
||
|
||
tc.getTax(100)
|
» |
"Sales tax on 100 = 7.5"
|
tc.getTax(250)
|
» |
"Sales tax on 250 = 18.75"
|
connection.downloadMP3("jitterbug") { |p| showProgress(p) } |
connection
is the receiver,
downloadMP3
is the name of the method, "jitterbug"
is
the parameter, and the stuff between the braces is the associated
block.
For class and module methods, the receiver will be the class or module
name.
File.size("testfile") Math.sin(Math::PI/4) |
self
, the current
object.
self.id
|
» |
537794160
|
id
|
» |
537794160
|
self.type
|
» |
Object
|
type
|
» |
Object
|
a = obj.hash # Same as a = obj.hash() # this. obj.someMethod "Arg1", arg2, arg3 # Same thing as obj.someMethod("Arg1", arg2, arg3) # with parentheses. |
def five(a, b, c, d, e)
|
||
"I was passed #{a} #{b} #{c} #{d} #{e}"
|
||
end
|
||
|
||
five(1, 2, 3, 4, 5 )
|
» |
"I was passed 1 2 3 4 5"
|
five(1, 2, 3, *['a', 'b'])
|
» |
"I was passed 1 2 3 a b"
|
five(*(10..14).to_a)
|
» |
"I was passed 10 11 12 13 14"
|
listBones("aardvark") do |aBone| # ... end |
if
or while
statement.
Sometimes, however, you'd like to be more flexible. For example, we
may be teaching math skills.[Of course, Andy and Dave would
have to learn math skills first. Conrad Schneiker reminded us that there are three kinds
of people: those who can count and those who can't.] The student
could ask for an n-plus table or an n-times table. If
the student asked for a 2-times table, we'd output 2, 4, 6, 8, and
so on. (This code does not check its inputs for errors.)
print "(t)imes or (p)lus: " times = gets print "number: " number = gets.to_i if times =~ /^t/ puts((1..10).collect { |n| n*number }.join(", ")) else puts((1..10).collect { |n| n+number }.join(", ")) end |
(t)imes or (p)lus: t number: 2 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 |
if
statement. If would be nice if we could factor out the
block that does the calculation.
print "(t)imes or (p)lus: " times = gets print "number: " number = gets.to_i if times =~ /^t/ calc = proc { |n| n*number } else calc = proc { |n| n+number } end puts((1..10).collect(&calc).join(", ")) |
(t)imes or (p)lus: t number: 2 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 |
Proc
object. It removes it from the parameter
list, converts the Proc
object into a block, and
associates it with the method.
This technique can also be used to add some syntactic sugar to block
usage. For example, you sometimes want to take an
iterator and store each value it yields into an array. We'll reuse our
Fibonacci number generator from page 40.
a = []
|
||
fibUpTo(20) { |val| a << val }
|
» |
nil
|
a.inspect
|
» |
"[1, 1, 2, 3, 5, 8, 13]"
|
into
, which
returns the block that fills the array. (Notice at the same time that
the block returned really is a closure---it references the parameter
anArray even after method into
has returned.)
def into(anArray)
|
||
return proc { |val| anArray << val }
|
||
end
|
||
|
||
fibUpTo 20, &into(a = [])
|
||
a.inspect
|
» |
"[1, 1, 2, 3, 5, 8, 13]"
|
SongList
.
class SongList def createSearch(name, params) # ... end end aList.createSearch("short jazz songs", { 'genre' => "jazz", 'durationLessThan' => 270 } ) |
=>
value pairs in an argument list, as long
as they follow any normal arguments and precede any array and block
arguments. All these pairs will be collected into a single hash and
passed as one argument to the method. No braces are needed.
aList.createSearch("short jazz songs", 'genre' => "jazz", 'durationLessThan' => 270 ) |
Previous < |
Contents ^
|
Next >
|