Class.new and inspect

Discussion in 'Ruby' started by Hal Fulton, Jan 23, 2004.

  1. Hal Fulton

    Hal Fulton Guest

    I'm curious as to how this little bit of magic works:

    hal@alpha ~/projects/peco $ irb
    irb(main):001:0> foo = Class.new(Array)
    => #<Class:0x40202470>
    irb(main):002:0> MyArray = Class.new(Array)
    => MyArray
    irb(main):003:0>

    In other words, when I create a class with Class.new, *and* assign it
    to a constant, somehow it knows its own name (via inspect), just as
    if I had done "class MyArray < Array".

    In the latter case, somehow it's more understandable to me.


    Perhaps even more perplexing:

    irb(main):007:0> A = B = C = Class.new(Hash)
    => B
    irb(main):008:0> x = C.new
    => {}
    irb(main):009:0> x.class
    => B

    What? Not A or C, the first or last, but B?


    Anyone?


    Hal
    Hal Fulton, Jan 23, 2004
    #1
    1. Advertising

  2. Hi,

    In message "Class.new and inspect"
    on 04/01/23, Hal Fulton <> writes:

    |Perhaps even more perplexing:
    |
    | irb(main):007:0> A = B = C = Class.new(Hash)
    | => B
    | irb(main):008:0> x = C.new
    | => {}
    | irb(main):009:0> x.class
    | => B
    |
    |What? Not A or C, the first or last, but B?

    When inspecting unnamed class, Ruby look around the constants for the
    name of the class, B this case is found first.

    matz.
    Yukihiro Matsumoto, Jan 23, 2004
    #2
    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. Darran Edmundson

    smart logging and the "inspect" module ...

    Darran Edmundson, May 28, 2005, in forum: Python
    Replies:
    1
    Views:
    278
    George Sakkis
    May 28, 2005
  2. Félix-Antoine Fortin

    inspect.stack() and frame

    Félix-Antoine Fortin, Mar 11, 2010, in forum: Python
    Replies:
    5
    Views:
    1,120
  3. Peng Yu
    Replies:
    0
    Views:
    283
    Peng Yu
    Jun 22, 2010
  4. deluxstar
    Replies:
    5
    Views:
    855
    Terry Reedy
    Sep 25, 2010
  5. Benny

    utf8 strings and inspect

    Benny, Jun 4, 2004, in forum: Ruby
    Replies:
    3
    Views:
    114
    Carlos
    Jun 4, 2004
Loading...

Share This Page