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:
puts "Hello!"puts("Hello!")
And so do these:
puts add_two 2puts 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 with the convention we’re using, which is:
-
Use parentheses for all method calls that take arguments, except for the
puts
andp
methods (and later,require
andinclude
). -
If a method doesn’t take any arguments, then don’t add empty parentheses. Simply omit them.
So, the idiomatic way to write the line above is:
puts add_two(2)
Methods without arguments
So far, we’ve also mentioned that methods sometimes don’t take any arguments. But we haven’t looked at an example. Here’s one:
def greetputs "Oh, hello!"endgreet
Explanation
-
The first three lines define a method, and we’ve picked the name
greet
for it. -
Line 5 consists of nothing but the word
greet
. When Ruby runs this code and finds the wordgreet
, it knows that this refers to the method defined earlier and calls it.
Ruby jumps into the method body, and this time, it doesn’t bring any objects with it as arguments because our method doesn’t need any. It then executes the line puts "Oh, hello!"
, which prints a greeting to the screen.
As we can see, we don’t use any parentheses here. We could, though. The following code would be perfectly valid and do exactly the same thing:
def greet()puts "Oh, hello!"endgreet()
However, the parentheses just add visual noise and make the code slightly less readable.
Consider what’s happening with the return value of our greet
method. We mentioned earlier that every method call always returns something (an object). So, we expect the greet
method call to return the object returned from the last evaluated statement (which, in our case, is puts "Oh, hello!
").
The puts
method always returns nil
because it was written that way. Its purpose is to print something to the screen, not return something interesting. It’s a command, not a question. So, the most sensible choice for a return value is nil.
If we look at our example code, we notice that we don’t do anything with the return value of the method called greet
. We don’t assign it to a variable, and we don’t pass it to another method call. In fact, we simply discard it because we’re not interested in it.
For the sake of demonstrating that puts
and greet
indeed return nil
, we can use p to inspect the return value, like so:
def greetputs "Oh, hello!"endp greet
Notice that it first prints the greeting using puts
, and then, after returning from the method, outputs the nil
value.