# Reflecting on objects without creating one

**URL:** <https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922>\
**Category:** ruby-talk\
**Created:** [20 December 2006 21:40 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922 "2006-12-20T21:40:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin6](https://avatars.discourse-cdn.com/v4/letter/k/aeb1de/32.png) [@Kevin6](https://rubytalk.org/u/Kevin6)\
**Post date:** [20 December 2006 21:40 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/1 "2006-12-20T21:40:05Z")

</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:** ![Jeremy\_McAnally1](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jeremy\_McAnally1](https://rubytalk.org/u/Jeremy_McAnally1)\
**Post date:** [20 December 2006 21:49 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/2 "2006-12-20T21:49:38Z")

</div>

(Class).superclass should do it.

Example:  
if a.superclass == b  
&nbsp;&nbsp;puts "Yes."  
else  
&nbsp;&nbsp;puts "No."  
end

--Jeremy

> **···**
>
> On 12/20/06, \_Kevin \<kevin.olbrich@gmail.com\> 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'?
> > 
> > \_Kevin

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [20 December 2006 21:52 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/3 "2006-12-20T21:52:02Z")

</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'?

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

Hope that helps.

James Edward Gray II

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

---

<div class="post-metadata">

**Author:** ![Justin\_Bailey](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Justin\_Bailey](https://rubytalk.org/u/Justin_Bailey)\
**Post date:** [20 December 2006 21:53 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/4 "2006-12-20T21:53:25Z")

</div>

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

> **···**
>
> On 12/20/06, \_Kevin \<kevin.olbrich@gmail.com\> 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'?

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ezra\_Zygmuntowicz](https://rubytalk.org/u/Ezra_Zygmuntowicz)\
**Post date:** [20 December 2006 22:05 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/5 "2006-12-20T22:05:40Z")

</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'?
> 
> 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

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

> **···**
>
> On Dec 20, 2006, at 1:53 PM, Justin Bailey wrote:
> 
> > On 12/20/06, \_Kevin \<kevin.olbrich@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Kevin6](https://avatars.discourse-cdn.com/v4/letter/k/aeb1de/32.png) [@Kevin6](https://rubytalk.org/u/Kevin6)\
**Post date:** [21 December 2006 00:25 UTC](https://rubytalk.org/t/reflecting-on-objects-without-creating-one/33922/6 "2006-12-21T00:25:06Z")

</div>

Thanks guys,

\_Kevin

Ezra Zygmuntowicz wrote:

> **···**
>
> > On Dec 20, 2006, at 1:53 PM, Justin Bailey wrote:
> > 
> > \> On 12/20/06, \_Kevin \<kevin.olbrich@gmail.com\> 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
> > 
> > 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 Zygmuntowicz  
> > -- Lead Rails Evangelist  
> > -- ez@engineyard.com  
> > -- Engine Yard, Serious Rails Hosting  
> > -- (866) 518-YARD (9273)
