What is the difference between "if" statements with "then" at the end?

What is the difference between these two Ruby if when we put then at the end of the if ?

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

and

 if(val == "hi") something.meth("hello") else something.meth("right") end 
+55
ruby
Jun 21 '10 at 9:55
source share
5 answers

then is the separator that helps Ruby identify the condition and the true part of the expression.

if condition then true-part else false-part end

then optional if you do not want to write the if expression on the same line. For an if-else end spanning multiple lines, the new line acts as a delimiter to split the conditional expression into 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 
+67
Jun 21 '10 at 10:02
source share

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.

+12
Jun 21 '10 at 11:29
source share

then is required only if you want to write an if on one line:

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

These brackets in your example are not significant, you can skip them anyway.

See Pickaxe Book for details .

+6
Jun 21 2018-10-10T00:
source share

There is no difference.

And, just FYI, your code can be optimized for

 something.meth( val == 'hi' ? 'hello' : 'right' ) 
+2
Jun 21 '10 at 9:56
source share

The only time that I like to use then in multiline if/else (yes, I know it is not required) is when there are several conditions for if , for example, like this:

 if some_thing? || (some_other_thing? && this_thing_too?) || or_even_this_thing_right_here? then some_record.do_something_awesome! end 

I find this to be much more readable than any of these (fully valid) options:

 if some_thing? || (some_other_thing? && this_thing_too?) || or_even_this_thing_right_here? some_record.do_something_awesome! end # or if some_thing? || (some_other_thing? && this_thing_too?) || or_even_this_thing_right_here? some_record.do_something_awesome! end 

Because it provides a visual distinction between the condition (s) of the if and the block executed if the condition (s) evaluates to true .

+2
Aug 01 '18 at 13:52
source share



All Articles