[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Better ruby way for caseing on class?

ebeard

10/4/2006 4:09:00 PM

I currently do this:

[Kernel,String,Object].each do |klass|
case klass.to_s
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


Is there a better way?

5 Answers

James Gray

10/4/2006 4:18:00 PM

0

On Oct 4, 2006, at 11:10 AM, ebeard wrote:

> I currently do this:
>
> [Kernel,String,Object].each do |klass|
> case klass.to_s
> 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
>
>
> Is there a better way?

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.

James Edward Gray II


Jano Svitok

10/4/2006 4:29:00 PM

0

On 10/4/06, James Edward Gray II <james@grayproductions.net> wrote:
> On Oct 4, 2006, at 11:10 AM, ebeard wrote:
>
> > I currently do this:
> >
> > [Kernel,String,Object].each do |klass|
> > case klass.to_s
> > 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
> >
> >
> > Is there a better way?
>
> 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.
>
> James Edward Gray II

His example is more strict: it will match only for Object object,
while yours will match any Object.

(in other words, the OP checks for identity, while you check for class
membership)

Robert Klemme

10/4/2006 4:37:00 PM

0

On 04.10.2006 18:08, ebeard wrote:
> I currently do this:
>
> [Kernel,String,Object].each do |klass|
> case klass.to_s
> 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
>
>
> Is there a better way?

Use the other form of "case":

[Kernel,String,Object].each do |klass|
case
when klass == Kernel :
puts 'I\'m doing stuff with object Kernel'
when klass == String :
puts 'I\'m doing stuff with object String'
when klass == Object :
puts 'I\'m doing stuff with object Object'
end
end

robert

Ara.T.Howard

10/4/2006 5:49:00 PM

0

ebeard

10/5/2006 6:42:00 PM

0

Hmmm. This is the most interesting approach.

I was not aware of Kernel#lambda.

It's been creeping around like a wolfda in lambda's clothing. :)

ara.t.howard@noaa.gov wrote:
> On Thu, 5 Oct 2006, ebeard wrote:
>
> > I currently do this:
> >
> > [Kernel,String,Object].each do |klass|
> > case klass.to_s
> > 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
> >
> >
> > Is there a better way?
>
> well, the code you have above is exactly equivalent to this
>
> [Kernel, String, Object].each{|k| puts "I'm doing stuff with object #{ k }"}
>
> but i suppose your real code is quite different? my point is just that
> looping over three things and selecting them each in turn is the same thing as
> pre-selecting the items to loop over. another alternative is
>
> actions = {
> Kernel => lambda{ puts "I\'m doing stuff with object Kernel"},
> String => lambda{ puts "I\'m doing stuff with object String"},
> Object => lambda{ puts "I\'m doing stuff with object Object"},
> }
>
> [Kernel, String, Object].each{|k| actions[k].call}
>
> which works for some situations and can sometimes be even more general
>
> actions = lambda{|k| lambda{ puts "I'm doing stuff with object #{ k }"} }
>
> [Kernel, String, Object].each{|k| actions[k].call}
>
>
> which is suited to compact arg dependant actions but not long ones.
>
> regards.
>
> -a
> --
> in order to be effective truth must penetrate like an arrow - and that is
> likely to hurt. -- wei wu wei