Here's a quick tip that is not directly related to your question: in Ruby there is no such thing as an if . In fact, there are no statements in Ruby. All this expression. The if expression returns the value of the last expression that was evaluated in the branch branch.
So no need to write
if condition foo(something) else foo(something_else) end
It would be better to write how
foo( if condition something else something_else end )
Or as a single line
foo(if condition then something else something_else end)
In your example:
something.meth(if val == 'hi' then 'hello' else 'right' end)
Note. Ruby also has a ternary operator ( condition ? then_branch : else_branch ), but this is completely unnecessary and should be avoided. The only reason a ternary operator is needed in languages โโlike C is because C if has an operator and therefore cannot return a value. You need a ternary operator, because this expression is the only way to return a value from a conditional. But in Ruby, if already an expression, so there is no need for a ternary operator.
Jรถrg W Mittag Jun 21 '10 at 11:29 2010-06-21 11:29
source share