How to simplify "a.instance_of? classX or a.instance_of? classY "

  • Thread starter Stephane Wirtel
  • Start date
S

Stephane Wirtel

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
 
E

Ezra Zygmuntowicz

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

[
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::SOAP::Address,
WSDL::Binding,
WSDL::SOAP::Binding,
WSDL::OperationBinding,
WSDL::SOAP::Operation,
WSDL::param,
WSDL::SOAP::Body,
WSDL::portType,
WSDL::Operation,
WSDL::SOAP::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 Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
P

Phrogz

Stephane said:
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
 
F

F. Senault

Le 4 novembre 2006 à 04:57, Phrogz a écrit :
Stephane said:
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

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
 
P

Phrogz

F. Senault said:
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

Very nice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top