Methods
Methods have 2 main uses:
Today, we're only looking at the asking part
Try this in IRB!
"I love Ruby".length
=> 11
Note: Length includes spaces!
Note: Return value is an Integer!
object.method
Try this in IRB!
"I love Ruby".reverse
=> "ybuR evol I"
Note: Return value is another String!
Try this in IRB!
"Ruby".upcase
=> "RUBY"
Try this in IRB!
"Ruby".downcase
=> "ruby"
Try this in IRB!
1.odd?
=> true
Try this in IRB!
-5.even?
=> false
?return either
trueor
false. They are yes/no questions!
Some methods require additional information
Try this in IRB!
"I love Ruby".include?("Ruby")
=> true
"I love Ruby".include?("Rails")
=> false
"I love Ruby".include?do?
include?requires extra information
object.method(argument)
Try this in IRB!
"I love Ruby".length.odd?
=> true
lengthand
odd?
"I love Ruby".length, which returns the integer
11and then it executes the method
odd?on that which returns the result
true
You don't have to know all of these method names by heart!
Googling helps! For example, google for "ruby doc string reverse"
One of the results will be: http://ruby-doc.org/core/String.html. This is the official Ruby documentation. Use Ctrl + F to find things on the website.
Another good source is Stackoverflow
What questions do you have?