Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Basic question about Ruby's class hierarchy

Reply
Thread Tools

Basic question about Ruby's class hierarchy

 
 
Patrick Li
Guest
Posts: n/a
 
      08-11-2008
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/.

 
Reply With Quote
 
 
 
 
Eric I.
Guest
Posts: n/a
 
      08-11-2008
On Aug 10, 11:22*pm, Patrick Li <(E-Mail Removed)> 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/arti...esClearly.html

Eric

====

Rails training and Ruby training available at http://LearnRuby.com .
On-site and customized training are available.
 
Reply With Quote
 
 
 
 
Chris Shea
Guest
Posts: n/a
 
      08-11-2008
On Aug 10, 9:22*pm, Patrick Li <(E-Mail Removed)> 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
 
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
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
(namespace) class Hierarchy Chart for .net 2.0 Mukund Patel ASP .Net 0 12-31-2004 02:03 PM
Invoking the Constructor of the Top Most Class in the Hierarchy from the Bottom most class H.MuthuKumaraRajan Java 3 02-04-2004 01:33 PM
static field in a class hierarchy Gael Java 2 12-10-2003 02:38 PM
Please comment on this class hierarchy design Avi Abrami Java 6 07-16-2003 12:54 AM



Advertisments