Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class.new and inspect

Reply
Thread Tools

Class.new and inspect

 
 
Hal Fulton
Guest
Posts: n/a
 
      01-23-2004
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




 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      01-23-2004
Hi,

In message "Class.new and inspect"
on 04/01/23, Hal Fulton <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inspect.stack() or inspect.currentframe() gives "list index out ofrange error" deluxstar Python 5 09-25-2010 05:12 PM
NAT and access lists and IP INSPECT JF Mezei Cisco 0 01-22-2010 11:24 AM
IP Inspect and Browsing issues KB Cisco 1 10-13-2007 06:31 PM
smart logging and the "inspect" module ... Darran Edmundson Python 1 05-28-2005 04:09 PM
ip inspect and access-list question didier Cisco 1 01-18-2004 01:02 AM



Advertisments