I recently discussed the following Ruby syntax with a colleague:
value = if a == 0 "foo" elsif a > 42 "bar" else "fizz" end
I have not seen such logic in person, but my colleague notes that this is actually quite common rubism. I tried to understand this topic and did not find the articles, pages or questions that discussed it, making me believe that this could be very important. However, another colleague considers the syntax confusing and would instead write the following logic:
if a == 0 value = "foo" elsif a > 42 value = "bar" else value = "fizz" end
The disadvantage is the repeated declaration of value = and the loss of the implicit else nil if we want to use it. It also seems to combine with many of the other syntactic properties of sugar found in Ruby.
My question is: how common is this method in Ruby? Is there any consensus on whether this community should be used or avoided?
source share