|
This isn't the first time you've hear of methods or used them, in fact, several code samples in the previous chapters were implemented with methods.
Methods are give other names too, such as functions,subprograms or procedures, while in other programming languages there might be several differences between all these, they all mean pretty much the same thing in Ruby, although everyone is trying to using the term "method" only.
Here's how you define a method:
def mymethod
puts "this is my first method"
end
How to call a method (when you call a method Ruby executes the statements in the method):
by giving it's name:
mymethod
How to pass arguments to a method
def showme(message)
puts message
end
message="ruby says hello"
showme(message)
Another example:
def showme(firstw,secondw)
puts firstw + " " + secondw + "!"
end
showme "Thank","you"
|