How to assign a variable based on a condition in Ruby
If you need to assign a variable based on a conditional, most programmers would do something like this:
result = nil
if condition == true
result = 1
else
result = 2
end
puts "The result is {result}."
I usually cringe when people say things like, “Oh, that’s a very C way of doing that”… but in this case, well, that’s a very C way of doing that.
Instead, a rubyist would write:
result = if condition == true
1
else
2
end
puts "The result is #{result}."