Skip to content

Instantly share code, notes, and snippets.

@mkoby
Created March 30, 2012 20:06
Show Gist options
  • Save mkoby/2254563 to your computer and use it in GitHub Desktop.
Save mkoby/2254563 to your computer and use it in GitHub Desktop.
Intro to Ruby - 06 - Methods Addendum
## Basic method with multiple arguments
def add_numbers(a, b)
a + b
end
add_numbers(5, 4) # => 9
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