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.
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 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.
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.
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’
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.
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.
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’
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
=> [[{{…}=>{…}}, {{…}=>{…}}]]
Here’s a simple conceptual model to think of if it seems a bit
obfuscated.
Go to your deskop and create a folder called myfolder.
open myfolder and create a folder inside it called myfolder.
Considering your desktop is TOP level much like main() is to many
programming languages and environments. Or how Class is an abstraction
for object oriented languages what Stucts are abstracted data types to
procedural languages.
If you want to blow your mind a bit you can create an alias, shortcut,
or softlink depending on operating system to reference (or point to)
the original myfolder.
Actually I’m not sure if that’s helpful. I’m better with fixing
problems that exist at a gestalt level. Maybe looking at ObjectSpace
may also give some hints (or a screen full of junk) on the
interpreters preprocessing state.
Rubinius is a smalltalk based vm
written in c++( iirc) at the bootstrap level and written in pure ruby
beyond that.
Minor nitpick: I think you’re confusing MagLev, which has a Smalltalk
VM base, and Rubinius, which has an LLVM base.
My bad. I can’t install MagLev on FreeBSD. Is it worth checking out.
For extension languages I prefer these interpreters to be able to run
everywhere. I don’t know how I was mislead. wikipedia mentions a short
stub on using the smalltalk bluebook but outside of using pry to
become inspired on some of the methods already in place in the
language I generally use mri and in some cases jruby. What’s the use
case for maglev? Fuse ruby with squeak?
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.
Could you tell me the black book name?
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.