[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Stéphane Wirtel

11/4/2006 2:07:00 AM

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
6 Answers

Ezra Zygmuntowicz

11/4/2006 2:18:00 AM

0


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

> 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
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Gavin Kistner

11/4/2006 3:57:00 AM

0

Stephane Wirtel 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

Trans

11/4/2006 5:22:00 AM

0

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

T.

F. Senault

11/4/2006 10:46:00 AM

0

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

> Stephane Wirtel 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

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
--
Young at heart an' it gets so hard to wait
When no one I know can seem to help me now
Old at heart but I mustn't hesitate
If I'm to find my way out (Guns n' Roses, Estranged)

Gavin Kistner

11/4/2006 3:02:00 PM

0

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

The CNY PIN GUY

11/4/2010 9:18:00 PM

0

On Nov 4, 2:26 pm, The CNY PIN GUY <cnypinball...@gmail.com> wrote:
> On Nov 4, 2:05 pm, nepinplayer <robski...@gmail.com> wrote:
>
> > On Nov 4, 1:53 pm, The CNY PIN GUY <cnypinball...@gmail.com> wrote:
>
> > > Used but in very nice shape
>
> > > 60 shipped UPS
> > > or 50 met up at pin fest Will be attending Friday only
>
> > > Email for interest and pics
>
> > > Thank you
> > > James
>
> > Email sent......
>
> Sale pending.
Sold!!