...

/

Arguments and Parentheses

Arguments and Parentheses

Learn the conventions for using parentheses when passing arguments to methods.

Notice how we’ve been calling so many of the methods in previous lessons by enclosing the arguments in parentheses—for example, add_two(3). But puts is also a method, and we’re quick to pass an argument to it without enclosing it in parentheses. For example, to print 5, we write puts 5.

That’s right. When we define or call (execute, use) a method, we can omit the parentheses.

So, these lines mean the same:

Press + to interact
Ruby
puts "Hello!"
puts("Hello!")

And so do these:

Press + to interact
Ruby
puts add_two 2
puts add_two(2)
puts(add_two 2)
puts(add_two(2))

When to use parentheses and when to omit them

There’s no clear rule about this, but there are some conventions. For now, we can stick ...