# How to simplify "a.instance\_of? classX or a.instance\_of? cla

**URL:** <https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893 "2006-11-07T11:00:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/1 "2006-11-07T11:00:10Z")

</div>

Hi all,

I would like to know if there is a way to simplify this code

```
object.instance_of? WSDL::XMLSchema::ComplexContent or
    object.instance_of? WSDL::XMLSchema::ComplexType or
    object.instance_of? WSDL::XMLSchema::SimpleType or
    object.instance_of? WSDL::XMLSchema::SimpleRestriction or
    object.instance_of? WSDL::XMLSchema::Element or
    object.instance_of? WSDL::XMLSchema::Sequence or
    object.instance_of? XSD::QName or
    object.instance_of? WSDL::Message or
    object.instance_of? WSDL::Part or
    object.instance_of? WSDL::Service or
    object.instance_of? WSDL::Port or
    object.instance_of? WSDL::SOAP::Address or
    object.instance_of? WSDL::Binding or
    object.instance_of? WSDL::SOAP::Binding or
    object.instance_of? WSDL::OperationBinding or
    object.instance_of? WSDL::SOAP::Operation or
    object.instance_of? WSDL::Param or
    object.instance_of? WSDL::SOAP::Body or
    object.instance_of? WSDL::PortType or
    object.instance_of? WSDL::Operation or
    object.instance_of? WSDL::SOAP::Attribute

```

?

Thanks

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/2 "2006-11-07T11:00:11Z")

</div>

On Nov 3, 2006, at 6:10 PM, Stephane W. wrote:

> ```
> object.instance_of? XSD::QName or
> object.instance_of? WSDL::SOAP::Body or
> object.instance_of? WSDL::PortType or
> object.instance_of? WSDL::Operation or
> object.instance_of? WSDL::SOAP::Attribute
> 
> ```
> 
> ?
> 
> Thanks

[  
WSDL::XMLSchema::ComplexContent,  
WSDL::XMLSchema::ComplexType,  
WSDL::XMLSchema::SimpleType,  
WSDL::XMLSchema::SimpleRestriction,  
WSDL::XMLSchema::Element,  
WSDL::XMLSchema::Sequence,  
XSD::QName,  
WSDL::Message,  
WSDL::Part,  
WSDL::Service,  
WSDL::Port,  
WSDL:🧼:Address,  
WSDL::Binding,  
WSDL:🧼:Binding,  
WSDL::OperationBinding,  
WSDL:🧼:Operation,  
WSDL::Param,  
WSDL:🧼:Body,  
WSDL::PortType,  
WSDL::Operation,  
WSDL:🧼:Attribute  
].any? {|cls| object.instance\_of? cls}

```
Thats one way to skin this cat. But I have to call code smell on

```

this whole check to see if this object is an instance of all these  
classes. Maybe there is an easier way to accomplish what you want if  
you expound on the question?

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/3 "2006-11-07T11:00:42Z")

</div>

If any are subclasses of the other’s then use only the superclass.

T.

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/4 "2006-11-07T11:01:03Z")

</div>

Le 4 novembre 2006 à 04:57, Phrogz a écrit :

> WSDL::XMLSchema::SimpleType  
> # your code here  
> end

Maybe more legible (and allows you to reuse the test) :

LIST = [ WSDL::XMLSchema::ComplexContent,  
WSDL::XMLSchema::ComplexType,  
WSDL::XMLSchema::SimpleType,  
…  
]  
…

case object  
when \*LIST  
…  
end

Fred

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/5 "2006-11-07T11:00:27Z")

</div>

Stephane W. wrote:

> I would like to know if there is a way to simplify this code
> 
> object.instance\_of? WSDL::XMLSchema::ComplexContent or  
> object.instance\_of? WSDL::XMLSchema::ComplexType or  
> object.instance\_of? WSDL::XMLSchema::SimpleType or  
> [snip]

case object  
when WSDL::XMLSchema::ComplexContent, WSDL::XMLSchema::ComplexType,  
WSDL::XMLSchema::SimpleType  
# your code here  
end

For example:

irb(main):001:0\> class Foo; end; class Bar; end; class Jimmy; end  
=\> nil  
irb(main):002:0\> o = Bar.new  
=\> #Bar:0x356f74  
irb(main):003:0\> case o; when Foo, Bar; p “yay”; else; p “boo”; end  
“yay”  
=\> nil  
irb(main):004:0\> o = Jimmy.new  
=\> #Jimmy:0x34416c  
irb(main):005:0\> case o; when Foo, Bar; p “yay”; else; p “boo”; end  
“boo”  
=\> nil

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/how-to-simplify-a-instance-of-classx-or-a-instance-of-cla/74893/6 "2006-11-07T11:01:27Z")

</div>

F. Senault wrote:

> when \*LIST  
> …  
> end
> 
> Fred

Very nice.
