5

I was recently discussing the following Ruby syntax with a colleague:

value = if a == 0
          "foo"
        elsif a > 42
          "bar"
        else
          "fizz"
        end

I haven't seen this sort of logic much personally, but my colleague notes that it's actually a fairly common Rubyism. I tried googling the topic and found no articles, pages, or SO questions discussing it, making me believe it might be a very matter-of-fact technique. Another colleague, however, finds the syntax confusing and would instead write the above logic like this:

if a == 0
  value = "foo"
elsif a > 42
  value = "bar"
else
  value = "fizz"
end

The disadvantage there being the repeated declarations of value = and the loss of an implicit else nil, if we wanted to use it. This also feels like it lines up with a lot of other syntactical sugar features found in Ruby.

My question is, how common is this technique in Ruby? Is there some sort of consensus on whether the community feels like this should be used or avoided?

3
  • 1
    I'd say, it's quite idiomatic. I use it often. Commented Jun 9, 2017 at 19:45
  • 2
    Note: this is not an if statement, it's an if expression, which is precisely why this works in the first place. (In fact, there are no statements in Ruby, everything is an expression and everything returns a value.) Commented Jun 9, 2017 at 23:51
  • It's similar to x = case(y); when .... and x = (y=3) ? 3 : 1, which are commonplace. Commented Jun 11, 2017 at 6:06

2 Answers 2

6
value = if condition
          x
        else
          y
        end

is common. It lends itself to cleaning up this situation:

if condition
  value = x
else
  value = y
end

Have a look at this Ruby style guide. It's a popular guide in how Ruby code should be written.

https://github.com/bbatsov/ruby-style-guide#use-if-case-returns

Sign up to request clarification or add additional context in comments.

1 Comment

When, as here, there's no elsif a ternary expression is compact and reads well: value = condition ? x : y.
4

The fact that if and case return values makes for some very tight, tidy, and yet still understandable code. It's a common pattern in Ruby when you're dealing with assignment through branching.

The way I tend to approach formatting these is to apply a level of indentation to make the assignment clear, but not overly "push" the code in too far:

value =
  if a == 0
    "foo"
  elsif a > 42
    "bar"
  else
    "fizz"
  end

Or if you want a case:

value =
  case
  when a == 0
    "foo"
  when a > 42
    "bar"
  else
    "fizz"
  end

In many cases you'll see a method that has an if as the body to determine the result:

def value(a)
  if a == 0
    "foo"
  elsif a > 42
    "bar"
  else
    "fizz"
  end
end

Then there's no quirky indentation necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.