Calling Methods on Objects
You may have also noticed a small tidbit in the last section:
When we called the meow
method on our cat
object, we didn’t use any paranthesis. Again, Ruby implies paranthesis so we don’t have to type them:
momma.meow
This holds true even when you’re passing arguments to a function. I’m going to add another method to our Cat
class that accepts an argument:
class Cat
def meow
puts 'meow'
end
def feed(food)
puts "noms on #{food}"
end
end
Time to feed Momma because her food bowl is almost a quarter empty and now she has crippling anxiety:
momma = Cat.new
momma.feed 'tuna'
=> 'noms on tuna'
See? We didn’t need to wrap feed
with paranthesis to call the method. But you absolutely can if you want to:
momma = Cat.new
momma.feed('tuna')
Sometimes, it’s more legible to read with paranthesis, I’ll leave that up to you decide.