tags:

views:

106

answers:

5

HI

whats the difference between these two ruby if statements when we put a then at the end of the if statement?

if(val == "hi") then
  something.meth("hello")
else
  something.meth("right")
end

and

if(val == "hi")
  something.meth("hello")
else
  something.meth("right")
end

thank you

+3  A: 

There's no difference at all.

And, just FYI, your code can be optimized to

something.meth( val == 'hi' ? 'hello' : 'right' )
zed_0xff
Shortened, instead of "optimized"?
Douglas
+1  A: 

There is no difference. "Then" often makes the code easier to read out loud, but has no other significance.

Mario Menger
+8  A: 

then is a delimiter to help ruby identify the condition and the true-part expr.

if condition then true-part else false-part end

then is optional unless you want to write an if expression in one line. For an if-else-end spanning multiple lines the newline acts as a delimiter to split the conditional from the true-part

# can't use newline as delimiter, need keywords
puts if (val == 1) then '1' else 'Not 1' end

# can use newline as delimiter
puts if (val == 1)
  '1'
else
  'Not 1'
end
Gishu
In Ruby, `if` is an expression, not a statement. In fact, *everything* is an expression, there are no statements. Thus, both your examples would be better written like `puts if val == 1 then '1' else 'Not 1' end`.
Jörg W Mittag
@Jorg - right. It'll take some time to scrub the years of C off me. :)
Gishu
+4  A: 

The then is only required if you want to write the if expression on one line:

if val == "hi" then something.meth("hello")
else something.meth("right")
end

That brackets in your example are not significant, you can skip them in either case.

See the Pickaxe Book for details.

Douglas
+1  A: 

Here's a quick tip that is not directly related to your question: in Ruby, there is no such thing as an if statement. In fact, in Ruby, there are no statements at all. Everything is an expression. An if expression returns the value of the last expression that was evaluated in the branch that was taken.

So, there is no need to write

if condition
  foo(something)
else
  foo(something_else)
end

This would better be written as

foo(
  if condition
    something
  else
    something_else
  end
)

Or as a one-liner

foo(if condition then something else something_else)

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 that is completely unnecessary and should be avoided. The only reason why the ternary operator is needed in languages like C is because in C if is a statement and thus cannot return a value. You need the ternary operator, because it is an expression and is the only way to return a value from a conditional. But in Ruby, if is already an expression, so there is really no need for a ternary operator.

Jörg W Mittag
In Ruby there are many ways to reach the same goal. I personally like the ternary operator. I find it compact and readable :)
nathanvda