Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Implicit object of a case statement

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

like image 987
Ethan Gunderson Avatar asked Feb 27 '23 07:02

Ethan Gunderson


1 Answers

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
like image 173
sepp2k Avatar answered Mar 07 '23 06:03

sepp2k