Ruby tip. Double association in a ternary operator

I found myself today in the opportunity to use a double association in a ternary operator. Basically I wanted to assign 2 variables based on a certain condition.

Normal ruby double assignment works easy like this:

#simple double assignment
a,b = 1,2
a  # 1
b  # 2

Up to here no surprise. But what if I want to evaluate a condition and assigns both the variables??

#double assignment . Wrong ways
a,b = true ? (1,2) : (3,4)
a,b = true ? 1,2  : 3,4

Both this attempts ends in a syntax error. The right way:

a,b = false ? [1,2] : [3,4]
a  # 3
b  # 4

Easy and clean. Values comes in an array and are correctly assigned. Of course this works for any number of variables you need to assign

# triple assignment in ternary operator
a,b,c = false ? [1,2,3] : [4,5,6]
a   # 4
b   # 5
c   # 6
comments powered by Disqus

Discussion, links, and tweets

I'm a freelance developer. Follow me on Twitter.