# Better ruby way for caseing on class?

**URL:** <https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018>\
**Category:** Ruby\
**Created:** [October 4, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018 "2006-10-04T18:10:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 4, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/1 "2006-10-04T18:10:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 4, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/2 "2006-10-04T18:18:54Z")

</div>

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

> 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 G. II

---

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 4, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/3 "2006-10-04T18:28:55Z")

</div>

On 10/4/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > when ‘Object’ :
> 
> James Edward G. 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)

---

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 4, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/4 "2006-10-04T18:40:57Z")

</div>

On 04.10.2006 18:08, ebeard wrote:

> 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
```

---

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 4, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/5 "2006-10-04T19:51:04Z")

</div>

On Thu, 5 Oct 2006, ebeard wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@ebeard](https://www.ruby-forum.com/u/ebeard)\
**Post date:** [October 5, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/better-ruby-way-for-caseing-on-class/71018/6 "2006-10-05T20:45:30Z")

</div>

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. 🙂
