[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Determining a class's ancestor easily without instances

Wes Gamble

7/12/2006 5:19:00 PM

Given two classes A and B, I know that I can loop through the
superclasses of A to determine it it's a descendant of B.

Is there a more expedient way to do this - something like "is_a?" but at
the class level (since is_a? is for objects)?

Thanks,
Wes

--
Posted via http://www.ruby-....

6 Answers

Joel VanderWerf

7/12/2006 5:22:00 PM

0

Wes Gamble wrote:
> Given two classes A and B, I know that I can loop through the
> superclasses of A to determine it it's a descendant of B.
>
> Is there a more expedient way to do this - something like "is_a?" but at
> the class level (since is_a? is for objects)?

if A < B
###
end

if A <= B
###
end

[A, B].sort

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Wes Gamble

7/12/2006 5:46:00 PM

0

Joel VanderWerf wrote:
> Wes Gamble wrote:
>> Given two classes A and B, I know that I can loop through the
>> superclasses of A to determine it it's a descendant of B.
>>
>> Is there a more expedient way to do this - something like "is_a?" but at
>> the class level (since is_a? is for objects)?
>
> if A < B
> ###
> end
>
> if A <= B
> ###
> end
>
> [A, B].sort

So is "<" the "superclass" assignment operator? Is it a method?

Seems like it operates differently in two contexts. It assigns in a
class declaration, but evaluates in a logical expression?

Am I understanding correctly?

Wes


--
Posted via http://www.ruby-....

Joel VanderWerf

7/12/2006 6:23:00 PM

0

Wes Gamble wrote:
> Joel VanderWerf wrote:
>> Wes Gamble wrote:
>>> Given two classes A and B, I know that I can loop through the
>>> superclasses of A to determine it it's a descendant of B.
>>>
>>> Is there a more expedient way to do this - something like "is_a?" but at
>>> the class level (since is_a? is for objects)?
>> if A < B
>> ###
>> end
>>
>> if A <= B
>> ###
>> end
>>
>> [A, B].sort
>
> So is "<" the "superclass" assignment operator? Is it a method?

It's a comparison operator, and yep, it's a method:

irb(main):012:0> string_lt = String.method("<")
=> #<Method: Class(Module)#<>
irb(main):013:0> string_lt.call File
=> nil
irb(main):014:0> string_lt.call Object
=> true

> Seems like it operates differently in two contexts. It assigns in a
> class declaration, but evaluates in a logical expression?

In "class Foo < Bar", the < is just syntax. It isn't being evaluated on
Foo and Bar.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Wes Gamble

7/12/2006 6:28:00 PM

0

I looked all over the Pickaxe reference to find it but I couldn't it.

I guess the "basic" operators are not documented, even when they're
overridden in clever ways?

Thanks,
Wes

--
Posted via http://www.ruby-....

Tim Hunter

7/12/2006 6:46:00 PM

0

Wes Gamble wrote:
> I looked all over the Pickaxe reference to find it but I couldn't it.
>
> I guess the "basic" operators are not documented, even when they're
> overridden in clever ways?
>
> Thanks,
> Wes

Look in the Pickaxe 1st Ed., in the documentation for the Module class,
under "Instance Methods." The <, <=, >, >= methods are documented.

"One module is considered greater than another if it is included in (or
is a parent class of) the other module. The other operators are defined
accordingly. If there is no relationship between the modules, returns
false for all operators."

--
Posted via http://www.ruby-....

Pit Capitain

7/12/2006 6:55:00 PM

0

Wes Gamble schrieb:
> I looked all over the Pickaxe reference to find it but I couldn't it.
>
> I guess the "basic" operators are not documented, even when they're
> overridden in clever ways?

Wes, you have to know that "Class" is a subclass of "Module", and so it
inherits all methods from "Module", among them the method "<". See


http://phrogz.net/ProgrammingRuby/ref_c_module.html#Module._lt_cm_lt_eq_cm_l...

The text says:

One module is considered greater than another if it is included in
*(or is a parent class of)* the other module.

Regards,
Pit