Looking for a way to include a ternary conditional inside a hash assignment.
a = 5
h = {}
h[:alpha] => a > 3 ? true : false # edited twice
h[:alpha] => (a > 3 ? true : false) # edited twice
There has to be a way of shortening this up.
You need to assign the values by using =
(assignment operator) and not =>
.
Try:
h[:alpha] = a > 3 ? true : false
Example:
2.1.2-perf :001 > a = 5
=> 5
2.1.2-perf :002 > h = {}
=> {}
2.1.2-perf :005 > h[:alpha] = (a > 3 ? true : false)
=> true
2.1.2-perf :006 > h[:alpha]
=> true
Edit (As per your comments):
2.1.2-perf :014 > user = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
2.1.2-perf :016 > user[1] == "solo" ? "#{user[2]} #{user[3]} (s)" : "#{user[4]} (g)"
=> "5 (g)"