# Determining a class's ancestor easily without instances

**URL:** <https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146>\
**Category:** Ruby\
**Created:** [July 12, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146 "2006-07-12T19:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/1 "2006-07-12T19:19:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/2 "2006-07-12T19:25:06Z")

</div>

Wes G. 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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/3 "2006-07-12T19:46:00Z")

</div>

Joel VanderWerf wrote:

> Wes G. 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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/4 "2006-07-12T20:23:09Z")

</div>

Wes G. wrote:

> > 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.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/5 "2006-07-12T20:45:27Z")

</div>

Wes G. 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.”

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/6 "2006-07-12T20:27:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 12, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/determining-a-classs-ancestor-easily-without-instances/98146/7 "2006-07-12T21:02:52Z")

</div>

Wes G. 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\_lt\_cm\_lt\_eq](http://phrogz.net/ProgrammingRuby/ref_c_module.html#Module._lt_cm_lt_eq_cm_lt_cm_lt_eq)

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
