# Reflecting on objects without creating one

**URL:** <https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035>\
**Category:** Ruby\
**Created:** [December 20, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035 "2006-12-20T22:42:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 20, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/1 "2006-12-20T22:42:24Z")

</div>

Let’s say I have two constants

a = Integer  
b = Numeric

Does anyone have any idea how to see if ‘a’ is a subclass of ‘b’  
WITHOUT creating an object of class ‘a’?

\_Kevin

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 20, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/2 "2006-12-20T22:50:26Z")

</div>

(Class).superclass should do it.

Example:  
if a.superclass == b  
puts “Yes.”  
else  
puts “No.”  
end

–Jeremy

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 20, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/3 "2006-12-20T22:52:27Z")

</div>

On Dec 20, 2006, at 3:40 PM, \_Kevin wrote:

> Let’s say I have two constants
> 
> a = Integer  
> b = Numeric
> 
> Does anyone have any idea how to see if ‘a’ is a subclass of ‘b’  
> WITHOUT creating an object of class ‘a’?

> > A = Integer  
> > =\> Integer
> 
> > B = Numeric  
> > =\> Numeric
> 
> > A.ancestors.include? B  
> > =\> true

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 20, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/4 "2006-12-20T22:54:27Z")

</div>

On 12/20/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Let’s say I have two constants
> 
> a = Integer  
> b = Numeric
> 
> Does anyone have any idea how to see if ‘a’ is a subclass of ‘b’  
> WITHOUT creating an object of class ‘a’?

irb(main):021:0\> a = Numeric  
=\> Numeric  
irb(main):022:0\> b = Integer  
=\> Integer  
irb(main):023:0\> a.ancestors  
=\> [Numeric, Comparable, Object, Kernel]  
irb(main):024:0\> a.ancestors.include? b  
=\> false  
irb(main):025:0\> b.ancestors.include? a  
=\> true  
irb(main):026:0\>

“b.ancestors.include? a” seems to do it. Notice I reversed your  
definitions  
of “a” & “b” (not for any particular reason, that’s just how it worked  
out).

Justin

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 21, 2006, 1:27am UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/5 "2006-12-21T01:27:00Z")

</div>

Thanks guys,

\_Kevin

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [December 20, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/reflecting-on-objects-without-creating-one/80035/6 "2006-12-20T23:06:31Z")

</div>

On Dec 20, 2006, at 1:53 PM, Justin B. wrote:

> irb(main):026:0\>
> 
> “b.ancestors.include? a” seems to do it. Notice I reversed your  
> definitions  
> of “a” & “b” (not for any particular reason, that’s just how it  
> worked out).
> 
> Justin

You can also use \< and \> to compare subclasses or superclasses

ez $ irb

> > a = Integer

# =\> Integer

> > b = Numeric

# =\> Numeric

> > a \< b

# =\> true

> > a \> b

# =\> false

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
