Re: Better ruby way for caseing on class?

From: James Edward G. II [mailto:[email protected]]

Sure. Drop the call to to_s() and the ’ … 's around the class
names. Case calls ===() for the condition object and Class defines
===() to do what you expect here.

Er…not quite, because (in his example) he has class objects, not
instances.

[Kernel,String,Object].each do |klass|
case klass
when Kernel
puts “I’m doing stuff with object Kernel”
when String
puts “I’m doing stuff with object String”
when Object
puts “I’m doing stuff with object Object”
end
end
#=> I’m doing stuff with object Kernel
#=> I’m doing stuff with object Kernel
#=> I’m doing stuff with object Kernel

ebeard, in reality are you trying to handle class objects in your case
statement, or do you have instances of that class that you’re trying to
determine the class of?

On Oct 4, 2006, at 11:27 AM, Gavin K. wrote:

From: James Edward G. II [mailto:[email protected]]

Sure. Drop the call to to_s() and the ’ … 's around the class
names. Case calls ===() for the condition object and Class defines
===() to do what you expect here.

Er…not quite, because (in his example) he has class objects, not
instances.

Egad, very good point. I apologize.

James Edward G. II

I’m actually dealing with classes. not instances thereof.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs