Anyone out there could help me to understand the "THE CLASS/OBJECT
CHICKEN-AND-EGG PARADOX". I am a newbie to this topic. The more I am
trying to get it making me confused. So anyway to get this one easily.
Also another topic making me confused "instance of class" and "instance
of object" - when the second one is pretty clear,but the first one still
not clear.
Google is your friend. Always at least try and Google something before
coming here.
Guess what I found when I googled the subject of this post (other than this
post - damn Google is fast)?
The stackoverflow answer is good.
···
On 20 February 2013 20:33, Xavier R. <lists@ruby-forum.com> wrote:
Hi,
Anyone out there could help me to understand the "THE CLASS/OBJECT
CHICKEN-AND-EGG PARADOX". I am a newbie to this topic. The more I am
trying to get it making me confused. So anyway to get this one easily.
Also another topic making me confused "instance of class" and "instance
of object" - when the second one is pretty clear,but the first one still
not clear.
Maybe you could provide more detail about what you are confused about. To
help clarify the second part.
Doing
class Foo
end
is similar to doing
Foo = Class.new
There you can see Foo is an instance of Class. Everything in ruby is object
(so to say), so Foo is still an instance of an object.
foo = Foo.new
This is an instance of Object, but not an instance of Class.
Hope this helps.
···
2013/2/20 Xavier R. <lists@ruby-forum.com>
Hi,
Anyone out there could help me to understand the "THE CLASS/OBJECT
CHICKEN-AND-EGG PARADOX". I am a newbie to this topic. The more I am
trying to get it making me confused. So anyway to get this one easily.
Also another topic making me confused "instance of class" and "instance
of object" - when the second one is pretty clear,but the first one still
not clear.
On Feb 20, 2013, at 12:33 , Xavier R. <lists@ruby-forum.com> wrote:
Anyone out there could help me to understand the "THE CLASS/OBJECT
CHICKEN-AND-EGG PARADOX". I am a newbie to this topic. The more I am
trying to get it making me confused. So anyway to get this one easily.
rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
RBASIC(rb_cClass)->klass
= RBASIC(rb_cModule)->klass
= RBASIC(rb_cObject)->klass
= RBASIC(rb_cBasicObject)->klass
= rb_cClass;
}
This is exactly what is happening. You can see it builds all of these
first AND then sets the klass AFTER they are created.
But to break it down further...
"every object has an internal record"
This is RBasic:
295 struct RObject {
296 struct RBasic basic;
297 struct st_table *iv_tbl;
298 };
290 struct RBasic {
291 unsigned long flags;
292 VALUE klass;
293 };
" of what class it’s an instance of,"
See how RObject has an RBasic, see how RBasic has a klass. When
rb_cObject is first created klass is a null value, later it sets that
value:
"and the internal record inside the object Class"
We're talking about RBasic again here
"points back to Class."
RBASIC(rb_cClass)->klass
= RBASIC(rb_cModule)->klass
= RBASIC(rb_cObject)->klass
= RBASIC(rb_cBasicObject)->klass
= rb_cClass;
That is done there.
If you want more details about how these things work check out the
following:
Maybe you could provide more detail about what you are confused about.
The class Class is an instance of itself; that is, it’s a Class object.
And there’s more.
Remember the class Object? Well, Object is a class—but classes are
objects. So,Object is an object. And Class is a class. And Object is a
class, and Class is an object.
Which came first? How can the class Class be created unless the class
Object
already exists? But how can there be a class Object (or any other class)
until there’s a class Class of which there can be instances?
This is a paragraph taken from David A. Black's book.The above paragraph
made me too confused.
It's a formal and direct explanation of the "Everything is and
Object". In the sense of "Turtles all the way down" not "I'm my own
grandpa!".
Review the syntax and experiment in irb with class, superclass,
ancestors on your own custom classes as well as the built in types
such as array, integers, strings. See if you can figure out how to
identify the methods associated within the inheritance chain at each
level from the instance methods to the class methods to the
superclasses methods and so forth.
Good luck
~Stu
···
On Thu, Feb 21, 2013 at 12:03 PM, Xavier R. <lists@ruby-forum.com> wrote:
In this regard please help me to understand the below :
"every object has an internal record of what class it’s an instance of,
and the internal record inside the object Class points back to Class."
It's not really a paradox. Take for example
irb(main):001:0> a = []
=> []
irb(main):002:0> a << a
=> [[...]]
How can a contain itself? The answer is that it's a reference. This is the
case for Class, the internal structure for determining the class of an
object holds a reference to itself
290 struct RBasic {
291 unsigned long flags;
292 VALUE klass;
293 };
See how klass is a VALUE, in the c-land VALUE is the equivalent of an
Object, so Class is an object where klass is set to point to itself.
You wouldn't really be able to create a class whose superclass is itself in
Ruby (at least nothing I can think of). I think this is often where people
get confused, the c implementation is not under the same restrictions as
the ruby implementation.
Please re-read the mail chain before asking questions. The below line can be found in the attached mail (last paragraph).
"This is a paragraph taken from David A. Black's book."
···
-----Original Message-----
From: Xavier R. [mailto:lists@ruby-forum.com]
Sent: Thursday, February 21, 2013 2:28 AM
To: ruby-talk ML
Subject: Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX
Matt Mongeau wrote in post #1098058:
Maybe you could provide more detail about what you are confused about.
The class Class is an instance of itself; that is, it’s a Class object.
And there’s more.
Remember the class Object? Well, Object is a class—but classes are
objects. So,Object is an object. And Class is a class. And Object is a
class, and Class is an object.
Which came first? How can the class Class be created unless the class
Object
already exists? But how can there be a class Object (or any other class)
until there’s a class Class of which there can be instances?
This is a paragraph taken from David A. Black's book.The above paragraph
made me too confused.
Please do not print this email unless it is absolutely necessary.
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
It is almost exactly the same with minor syntactic differences. Remove the rb_c's and semicolons, switch "->" to "." and you have pretty much the same thing in ruby.
···
On Feb 21, 2013, at 11:34 , "Xavier R." <lists@ruby-forum.com> wrote:
You are awesome! But weak in C code :(. So can the same explanation be
made using Ruby code?
It's not really a paradox. Take for example
irb(main):001:0> a =
=>
irb(main):002:0> a << a
=> [[...]]
irb(main):003:0> a.flatten!
ArgumentError: tried to flatten recursive array
from (irb):3:in `flatten!'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Ha! I bet whoever wrote that ( Matz, presumably? ) was shaking his head
and thinking "Well this is one error message no sane programmer will
ever see." I'd never have seen that as a possibility.
Matt Mongeau wrote in post #1098076:
> It's not really a paradox. Take for example
> irb(main):001:0> a =
> =>
> irb(main):002:0> a << a
> => [[...]]
>
Is it possible to give some small ruby code in-contrast with C - code
for better understanding.
It's not really a paradox. Take for example
irb(main):001:0> a =
=>
irb(main):002:0> a << a
=> [[...]]
irb(main):003:0> a.flatten!
ArgumentError: tried to flatten recursive array
from (irb):3:in `flatten!'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Ha! I bet whoever wrote that ( Matz, presumably? ) was shaking his head
and thinking "Well this is one error message no sane programmer will
ever see." I'd never have seen that as a possibility.
For more object reference shenanigans please enjoy
irb(main):005:0> h = {}
=> {}
irb(main):006:0> h[h] = h
=> {{...}=>{...}}
irb(main):007:0> h.to_a
=> [[{{...}=>{...}}, {{...}=>{...}}]]
Meta-Programming Ruby is a decent book which explains a bit better on
your question. Though Blacks book is a better book for just starting
ruby it mainly is just a beginners book.
It may be helpful to understand what a Module is in ruby and how it's
used with classes in ruby's object model. Also you'll want to
investigate what a Eigenclass class is and what singleton methods are.
Also note how ruby's reflection and method lookup syntax may always
be telling the whole truth. Mainly this has to do with scoping rules
and hidden classes as well to allow embedded languages within ruby
which can be seen in ruby's recursive style iteration syntax
methodology.
One way to visualize it outside of the repl would be to consider that
lookup begins at the instance object's receiver and moves to the right
and then up.
Showing the C code would be to see what the actual implementation is
doing. If you were to view it as a flattened set of instructions a
reference pointer would simply be a hash which simply is a two
dimensional array.
If your interested in reading ruby implemented in ruby instead of c
check out Rubinius and install pry. Rubinius is a smalltalk based vm
written in c++( iirc) at the bootstrap level and written in pure ruby
beyond that.
Of course if object based programming is new to you it's also
necessary to grok what self is and how message sending and
communication works. Consider it just the one of the many programming
models which ruby has at the end users disposal.
~Stu
···
On Wed, Feb 20, 2013 at 4:03 PM, Xavier R. <lists@ruby-forum.com> wrote:
Matt Mongeau wrote in post #1098076:
It's not really a paradox. Take for example
irb(main):001:0> a =
=>
irb(main):002:0> a << a
=> [[...]]
Is it possible to give some small ruby code in-contrast with C - code
for better understanding.