I would like to access the case statements expression from within a then clause i.e.
food = "cheese"
case food
when "dip" then "carrot sticks"
when "cheese" then "#{expr} crackers"
else
"mayo"
end
where in this case expr would be the current value of food. In this case I know, I could simply access the variable food, however there may be cases where the value is no longer accessible (array.shift etc..). Other than moving the expr out into a local variable and then accessing it, is there a way of directly accessing the value of the cases expr?
Roja
p.s. I know that this specific example is trivial its mealy an example scenario.
#!/usr/bin/ruby1.8
a = [1, 2, 3]
case value = a.shift
when 1
puts "one (#{value})"
when 2
puts "two (#{value})"
end
# => one (1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With