Created
March 30, 2012 20:06
-
-
Save mkoby/2254563 to your computer and use it in GitHub Desktop.
Intro to Ruby - 06 - Methods Addendum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Basic method with multiple arguments | |
def add_numbers(a, b) | |
a + b | |
end | |
add_numbers(5, 4) # => 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def square(number) | |
number * number | |
end | |
## Here we call the square method without | |
## parentheses and Ruby handles it fine. | |
square 8 # => 64 | |
## This even works with multiple arguments. | |
## Using the add_number method created above. | |
add_number 5, 4 # => 9 | |
## You want to be careful when not using parentheses | |
## and you should always use them when you're calling | |
## a method on the result of another method | |
s = "Michael's got a string" | |
s.sub("string", "guitar").downcase | |
## If you do s.sub 'string', 'guitar'.downcase | |
## it won't work as you might expect because Ruby | |
## wouldn't know how to parse it properly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment