Finding the super class of an object?

How can I find out what the super class is of an object, Say if a class
extends Hash. I need to be able to find a string representation of the
super class ('Hash').... is_a? won't work as that requires an actual
module or class.. I need a string representation.

any ideas? thanks..

···

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

Hi --

How can I find out what the super class is of an object, Say if a class
extends Hash. I need to be able to find a string representation of the
super class ('Hash').... is_a? won't work as that requires an actual
module or class..

is_a? won't report an inheritance relationship in any case:

   class D < C; end
   D.is_a?(C) # false (unless C is Object maybe....)

I need a string representation.

any ideas? thanks..

superclass.name will give it to you as a string.

David

···

On Tue, 16 Jan 2007, Aaron Smith wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Aaron Smith wrote:

How can I find out what the super class is of an object, Say if a class
extends Hash. I need to be able to find a string representation of the
super class ('Hash').... is_a? won't work as that requires an actual
module or class.. I need a string representation.

any ideas? thanks.

Will Module#ancestors work?

irb(main):001:0> class A
irb(main):002:1> end
=> nil
irb(main):003:0> class B < A
irb(main):004:1> end
=> nil
irb(main):005:0> A.ancestors
=> [A, Object, Kernel]
irb(main):006:0> B.ancestors
=> [B, A, Object, Kernel]

-Justin

yes, is_a? does report inheritance. consider this...

class C
end

class D < C
end

t = D.new

puts t.is_a?(C)

thanks.

···

On 1/15/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Tue, 16 Jan 2007, Aaron Smith wrote:

> How can I find out what the super class is of an object, Say if a class
> extends Hash. I need to be able to find a string representation of the
> super class ('Hash').... is_a? won't work as that requires an actual
> module or class..

is_a? won't report an inheritance relationship in any case:

   class D < C; end
   D.is_a?(C) # false (unless C is Object maybe....)

> I need a string representation.
>
> any ideas? thanks..

superclass.name will give it to you as a string.

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

do you know of anyway to do this on an instance of an object, not on the
class method??

thanks..

···

On 1/15/07, aaron smith <beingthexemplarylists@gmail.com> wrote:

yes, is_a? does report inheritance. consider this...

class C
end

class D < C
end

t = D.new

puts t.is_a?(C)

thanks.

On 1/15/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
> Hi --
>
> On Tue, 16 Jan 2007, Aaron Smith wrote:
>
> > How can I find out what the super class is of an object, Say if a
> class
> > extends Hash. I need to be able to find a string representation of the
> > super class ('Hash').... is_a? won't work as that requires an actual
> > module or class..
>
> is_a? won't report an inheritance relationship in any case:
>
> class D < C; end
> D.is_a?(C) # false (unless C is Object maybe....)
>
> > I need a string representation.
> >
> > any ideas? thanks..
>
> superclass.name will give it to you as a string.
>
> David
>
> --
> Q. What is THE Ruby book for Rails developers?
> A. RUBY FOR RAILS by David A. Black ( http://www.manning.com/black\)
> (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
> A. Ruby Power and Light, LLC (http://www.rubypal.com)
>

Hi --

Hi --

> How can I find out what the super class is of an object, Say if a class
> extends Hash. I need to be able to find a string representation of the
> super class ('Hash').... is_a? won't work as that requires an actual
> module or class..

is_a? won't report an inheritance relationship in any case:

   class D < C; end
   D.is_a?(C) # false (unless C is Object maybe....)

yes, is_a? does report inheritance. consider this...

class C
end

class D < C
end

t = D.new

puts t.is_a?(C)

I interpreted your question as meaning that, given a class, you wanted
a string representation of its superclass -- and my point was that
is_a? isn't relevant in that situation, because D.is_a?(C) is false.
(It's actually not relevant anyway, since it's a boolean query; it
returns true or false, which isn't what you're looking for.)

If you want to go from any object to its class's superclass in string
form, you would do:

   obj.class.superclass.name

David

···

On Tue, 16 Jan 2007, aaron smith wrote:

On 1/15/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

On Tue, 16 Jan 2007, Aaron Smith wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

nevermind, got it..

t.class.superclass.name

···

On 1/15/07, aaron smith <beingthexemplarylists@gmail.com> wrote:

do you know of anyway to do this on an instance of an object, not on the
class method??

thanks..

On 1/15/07, aaron smith < beingthexemplarylists@gmail.com> wrote:
>
> yes, is_a? does report inheritance. consider this...
>
> class C
> end
>
> class D < C
> end
>
> t = D.new
>
> puts t.is_a?(C)
>
> thanks.
>
> On 1/15/07, dblack@wobblini.net < dblack@wobblini.net> wrote:
> >
> > Hi --
> >
> > On Tue, 16 Jan 2007, Aaron Smith wrote:
> >
> > > How can I find out what the super class is of an object, Say if a
> > class
> > > extends Hash. I need to be able to find a string representation of
> > the
> > > super class ('Hash').... is_a? won't work as that requires an actual
> >
> > > module or class..
> >
> > is_a? won't report an inheritance relationship in any case:
> >
> > class D < C; end
> > D.is_a?(C) # false (unless C is Object maybe....)
> >
> > > I need a string representation.
> > >
> > > any ideas? thanks..
> >
> > superclass.name will give it to you as a string.
> >
> > David
> >
> > --
> > Q. What is THE Ruby book for Rails developers?
> > A. RUBY FOR RAILS by David A. Black ( http://www.manning.com/black\)
> > (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf
> > )
> > Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
> > A. Ruby Power and Light, LLC (http://www.rubypal.com)
> >
>

irb(main):013:0> a=10
=> 10
irb(main):014:0> a.class.ancestors
=> [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]
irb(main):015:0> a.class.superclass.name
=> "Integer"
irb(main):016:0>