Basic question about Ruby's class hierarchy

Discussion in 'Ruby' started by Patrick Li, Aug 11, 2008.

  1. Patrick Li

    Patrick Li Guest

    Hi,
    I thought I understood this, but apparently not.

    What is this object exactly?
    o = class << String; self; end

    From my understanding:
    p o
    and
    p String
    should output the same thing.

    but it doesn't
    p o #<Class:String>
    p String #String

    Thanks for answering such a basic question for me.
    -Patrick
    --
    Posted via http://www.ruby-forum.com/.
     
    Patrick Li, Aug 11, 2008
    #1
    1. Advertising

  2. Patrick Li

    Eric I. Guest

    On Aug 10, 11:22 pm, Patrick Li <> wrote:
    > I thought I understood this, but apparently not.
    >
    > What is this object exactly?
    > o = class << String; self; end
    >
    > From my understanding:
    > p o
    > and
    > p String
    > should output the same thing.
    >
    > but it doesn't
    > p o #<Class:String>
    > p String #String


    o is not the same as String. o is String's metaclass. The exact role
    that metaclasses play is Ruby's take on object-oriented programming is
    too much for an answer post such as this. But you might want to read
    why's explanation at:

    http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html

    Eric

    ====

    Rails training and Ruby training available at http://LearnRuby.com .
    On-site and customized training are available.
     
    Eric I., Aug 11, 2008
    #2
    1. Advertising

  3. Patrick Li

    Chris Shea Guest

    On Aug 10, 9:22 pm, Patrick Li <> wrote:
    > Hi,
    > I thought I understood this, but apparently not.
    >
    > What is this object exactly?
    > o = class << String; self; end
    >
    > From my understanding:
    > p o
    > and
    > p String
    > should output the same thing.
    >
    > but it doesn't
    > p o #<Class:String>
    > p String #String
    >
    > Thanks for answering such a basic question for me.
    >   -Patrick
    > --
    > Posted viahttp://www.ruby-forum.com/.


    In your example, String is an instance of Class, and o is the
    eigenclass (or metaclass) of String. Maybe this will help clarify:

    o = class << String; self; end
    o.class_eval do
    def backwards
    'gnirtS'
    end
    end

    String.backwards # => "gnirtS"

    HTH,
    Chris
     
    Chris Shea, Aug 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mukund Patel
    Replies:
    0
    Views:
    1,731
    Mukund Patel
    Dec 31, 2004
  2. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    450
    H.MuthuKumaraRajan
    Feb 4, 2004
  3. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  4. Mehr, Assaph (Assaph)
    Replies:
    1
    Views:
    499
    Simon Strandgaard
    Apr 15, 2004
  5. Jason Lillywhite

    ruby class hierarchy

    Jason Lillywhite, Aug 12, 2009, in forum: Ruby
    Replies:
    0
    Views:
    83
    Jason Lillywhite
    Aug 12, 2009
Loading...

Share This Page