[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

difference?

Gábor Sebestyén

7/14/2005 2:49:00 PM

Hi,

What is the difference between

obj.kind_of?(Customer)

and

obj.class == Customer.class

?
The earlier works but the latter don't. Why?
Thanks,

Gábor

"Never trust a computer you can't throw out a window." - Steve Wozniak

14 Answers

threeve.org

7/14/2005 2:53:00 PM

0

On 7/14/05, Gábor SEBESTYÉN <segabor@gmail.com> wrote:
> Hi,
>
> What is the difference between
>
> obj.kind_of?(Customer)
>
> and
>
> obj.class == Customer.class
>
> ?
> The earlier works but the latter don't. Why?
> Thanks,


obj.class == Customer

Customer.class == Class


James Gray

7/14/2005 2:55:00 PM

0

On Jul 14, 2005, at 9:49 AM, Gábor SEBESTYÉN wrote:

> Hi,
>
> What is the difference between
>
> obj.kind_of?(Customer)
>
> and
>
> obj.class == Customer.class
>
> ?
> The earlier works but the latter don't. Why?

Customer is a "class", so when you ask for Customer.class you're
probably getting Class as an answer. I believe what you meant is:

obj.class == Customer

Another way to write that is using the "case equals" method (used to
resolve case statements):

Customer === obj

Hope that helps.

James Edward Gray II



Gábor Sebestyén

7/14/2005 3:04:00 PM

0


On 2005.07.14., at 16:55, James Edward Gray II wrote:

> obj.class == Customer
>
Indeed, that's what I wanted.
Thanks,

Gábor

"Reklám - Ez az emil reciklált elektronok felhasználásával készült!"

Robert Klemme

7/14/2005 3:13:00 PM

0

James Edward Gray II wrote:
> On Jul 14, 2005, at 9:49 AM, Gábor SEBESTYÉN wrote:
>
>> Hi,
>>
>> What is the difference between
>>
>> obj.kind_of?(Customer)
>>
>> and
>>
>> obj.class == Customer.class
>>
>> ?
>> The earlier works but the latter don't. Why?
>
> Customer is a "class", so when you ask for Customer.class you're
> probably getting Class as an answer. I believe what you meant is:
>
> obj.class == Customer
>
> Another way to write that is using the "case equals" method (used to
> resolve case statements):
>
> Customer === obj
>
> Hope that helps.
>
> James Edward Gray II

Just for the sake of completeness: obj.kind_of? Customer is equivalent to
Customer === obj while obj.class == Customer has different semantics. The
latter form test for the exact class only while both former variants test
for classes and sub classes.

Kind regards

robert

mathew

7/14/2005 6:31:00 PM

0

Robert Klemme wrote:
> Just for the sake of completeness: obj.kind_of? Customer is equivalent to
> Customer === obj while obj.class == Customer has different semantics. The
> latter form test for the exact class only while both former variants test
> for classes and sub classes.

....and since testing for a specific class prohibits substituting in
derived classes, and hence removes a lot of the power of dynamic OO
programming, it's best to use kind_of? --unless you have a real need to
check for only the precise class specified.

And it's often better still to use respond_to? to check for the specific
behaviors you need, and get the benefits of Duck Typing, rather than
check for classes.


mathew

Gene Tani

7/15/2005 12:48:00 AM

0

Hmm, seems to be few ways to test/monitor
membership/inheritance/mix-in-ness:

Object#kind_of?, #is_a?, #instance_of?, #type (deprecated), #class

Module#ancestors, #included_modules

Class#inherited

MyClassname === myobj

Daniel Brockman

7/15/2005 2:29:00 AM

0

Gene Tani

7/15/2005 7:26:00 AM

0

Listened to Alex Martelli talk about decorators, metaclasses and method
resolution order in Python. Not easy to draw direct parallels to ruby

jason r tibbetts

7/15/2005 2:32:00 PM

0

Daniel Brockman wrote:
> "Gene Tani" <gene.tani@gmail.com> writes:
>
>
>>Hmm, seems to be few ways to test/monitor
>>membership/inheritance/mix-in-ness:
>
>
> Indeed. :-)
>
>
>>Object#kind_of?, #is_a?,
>>
>>Module#ancestors,
>>
>>MyClassname === myobj
>
>
> Interestingly, you've listed these in order of preference:
>
> * foo.kind_of? Foo --- perfect
> * foo.is_a? Foo --- unrubyish due to `is_*?'

What's unrubyish about is_a?




Daniel Brockman

7/15/2005 4:08:00 PM

0