[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Better ruby way for caseing on class?

Gavin Kistner

10/4/2006 4:28:00 PM

From: James Edward Gray II [mailto:james@grayproductions.net]
> 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?

2 Answers

James Gray

10/4/2006 4:32:00 PM

0

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

> From: James Edward Gray II [mailto:james@grayproductions.net]
>> 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 Gray II

ebeard

10/5/2006 6:33:00 PM

0

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


Gavin Kistner wrote:
> From: James Edward Gray II [mailto:james@grayproductions.net]
> > 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?