views:

84

answers:

1

In Ruby, is there a way to get the implicit object of a case statement?

case 2+2
when '2'
  puts '2'
else
  puts "#{some_object}"
end

Where 'some_object' would be the return value of whatever statement was evaluated by case

+8  A: 

No there's not. You'll have to do something like:

some_object = 2+2
case some_object
when '2'
  puts '2'
else
  puts some_object
end

or

case some_object = 2+2
when '2'
  puts '2'
else
  puts some_object
end
sepp2k