You'll have to think about the Ruby parser.
This will work fine:
age >= 21 ? puts("You're over 21!") : puts("You're too young.")
The reason is that if you use the puts
method without brackets, the parser doesn't understand the :
token that comes afterwards; it doesn't realise that it is associated with the ternary operator earlier. You can also do this:
puts age >= 21 ? "You're over 21!" : "You're too young."
This is fine -- the parser doesn't run into any ambiguities here.
You can also do this:
puts "You're #{ age >= 21 ? 'over 21!' : 'too young.' }"
Here's why the ambiguity happens. Let's say you have two functions, x
and a
, like this:
2.0.0-p195 :033 > def x(y)
2.0.0-p195 :034?> y + 1
2.0.0-p195 :035?> end
=> nil
2.0.0-p195 :036 > def a(b)
2.0.0-p195 :037?> b + 2
2.0.0-p195 :038?> end
=> nil
The brackets don't make a difference here:
2.0.0-p195 :039 > x a 1
=> 4
2.0.0-p195 :040 > x a(1)
=> 4
2.0.0-p195 :041 > x(a(1))
=> 4
2.0.0-p195 :042 > x(a 1)
=> 4
But if x
could take more than one parameter? Now you have to help the parser out:
2.0.0-p195 :043 > def x(y, z)
2.0.0-p195 :044?> y + z + 1
2.0.0-p195 :045?> end
Let's make a call without brackets like before:
2.0.0-p195 :047 > x a 1, 2
Are you passing a(1), 2
to x
, or are you trying to call a(1, 2)
and pass the result to x
? By convention Ruby assumes you are trying to call a(1, 2)
, but a
only takes one argument, so you get an error:
ArgumentError: wrong number of arguments (2 for 1)
from (irb):36:in `a'
So you need brackets:
2.0.0-p195 :052 > x a(1), 2
=> 6