I used to implement it in Ruby using the ternary operator,||I think that using is shorter and more readable. (Personally)
For example, suppose you want to assign the value a to a variable x if the value a is not nil, and 1 if the value a is nil. In that case, the ternary operator can be implemented as follows.
x = !a.nil? ? a : 1
||Can be implemented as follows.
x = a || 1
||Then, since it is evaluated in order from the left, the evaluation target is false(nil or false)If so, move to the right and return the first true one.
What do you think. I think there are likes and dislikes, but in the above cases,||I think it's cleaner to use.
Recommended Posts