# "obj.is\_one\_of? {Class1, Class2, ...}" as similar to "obj.is\_a?(Class1) or obj.is\_a?(Class2)"

**URL:** <https://www.ruby-forum.com/t/obj-is-one-of-class1-class2-as-similar-to-obj-is-a-class1-or-obj-is-a-class2/157934>\
**Category:** Rails\
**Created:** [January 16, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/obj-is-one-of-class1-class2-as-similar-to-obj-is-a-class1-or-obj-is-a-class2/157934 "2009-01-16T18:22:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmfreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@jmfreitas](https://www.ruby-forum.com/u/jmfreitas)\
**Post date:** [January 16, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/obj-is-one-of-class1-class2-as-similar-to-obj-is-a-class1-or-obj-is-a-class2/157934/1 "2009-01-16T18:22:08Z")

</div>

I need to check if a given object is an instance of a range of  
Classes, and I was wondering if I could do it by passing in is\_a? an  
hash of classes, instead of calling is\_a? for each class I have to  
compare…  
As I state in the subject (obj.is\_one\_of? {Class1, Class2, …}), this  
could justify a method “obj.is\_one\_of?” which returns true if the  
object “obj” is an instance of Class1 or Class2…  
Is there something that I’m missing?

---

<div class="post-metadata">

**Author:** ![jmfreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@jmfreitas](https://www.ruby-forum.com/u/jmfreitas)\
**Post date:** [January 16, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/obj-is-one-of-class1-class2-as-similar-to-obj-is-a-class1-or-obj-is-a-class2/157934/2 "2009-01-16T19:36:11Z")

</div>

Thanks so much for the quick and useful reply…  
The suggestion to use “case obj when Class1, Class2” completely  
fulfills my needs, most appreciated 😃

---

<div class="post-metadata">

**Author:** ![jmfreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@jmfreitas](https://www.ruby-forum.com/u/jmfreitas)\
**Post date:** [January 16, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/obj-is-one-of-class1-class2-as-similar-to-obj-is-a-class1-or-obj-is-a-class2/157934/3 "2009-01-16T18:40:20Z")

</div>

On Jan 16, 2009, at 12:21 PM, jmfreitas wrote:

> I need to check if a given object is an instance of a range of  
> Classes,

Are you sure? It would be more idiomatic to check for duck-typing:  
obj.respond\_to? :my\_method  
or to just call it and deal with the consequences:  
begin  
obj.my\_method  
rescue NoMethodError  
# deal with it  
end

You already have something similar with:  
case obj  
when Class1, Class2  
# Yippee!  
else  
# Drat!  
end

> and I was wondering if I could do it by passing in is\_a? an  
> hash of classes, instead of calling is\_a? for each class I have to  
> compare…  
> As I state in the subject (obj.is\_one\_of? {Class1, Class2, …}), this  
> could justify a method “obj.is\_one\_of?” which returns true if the  
> object “obj” is an instance of Class1 or Class2…  
> Is there something that I’m missing?

What are you trying to do that causes you to want this?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
