[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[EVALUATION] - E03b - The Ruby Object Model

Ilias Lazaridis

4/5/2005 12:34:00 PM

[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
http://groups-beta.google.com/group/comp.lang.ruby/msg/412943...

-

In the above thread, I had some problems with the Ruby Object Model.

I currently try to create an UML diagramm, to add this to the evaluation
result:

http://laz.../case/lang...

-

The existing documentation seems at least missleading, if not wrong:

-

cmd:> ri Class


"Classes, modules, and objects are interrelated. In the diagram that
follows, the arrows represent inheritance, and the parentheses
meta-classes. All metaclasses are instances of the class `Class'."

+------------------+
| |
Object---->(Object) |
^ ^ ^ ^ |
| | | | |
| | +-----+ +---------+ |
| | | | |
| +-----------+ | |
| | | | |
+------+ | Module--->(Module) |
| | ^ ^ |
OtherClass-->(OtherClass) | | |
| | |
Class---->(Class) |
^ |
| |
+----------------+

-

this can be drawn like this:

otherClass--->(OtherClass)
| |
| |
v v
Object--->(Object)
^ ^^
| ||
| / |
| / |
| / |
| / |
Module--->(Module)
^ / ^
| / |
|/ |
Class---->(Class)

-

i've played within IRB, but:

I cannot access any of the metaclasses

the same in the documentation: no metaclasses

-

Any suggestions / clarifications?

.

--
http://laz...
18 Answers

Austin Ziegler

4/5/2005 2:05:00 PM

0

On Apr 5, 2005 8:34 AM, Ilias Lazaridis <ilias@lazaridis.com> wrote:
> The existing documentation seems at least missleading, if not
> wrong:

It's neither misleading nor wrong. It is complex.

> this can be drawn like this:


> i've played within IRB, but:
> I cannot access any of the metaclasses
> the same in the documentation: no metaclasses

puts Class.id # => 20927668
puts class << Class; self.id; end # => 20927632
puts Object.id # => 20927692
puts class << Object; self.id; end # => 20927656

The class "Class" has an object ID that is different than its
metaclass. The same with class "Object". Objects, too, can have
metaclasses, like:

foo = Object.new
puts foo.id # => 22734748
puts class << foo; self.id; end # => 22725040

I've modified your diagram; ocObject* is an instance of otherClass,
and the (ocObject)* metaclass is the metaclass for this instance,
and it is a different metaclass than any other instance of
otherClass.

ocObject*---->(ocObject)*
| |
| |
v v
otherClass--->(OtherClass)
| |
| |
v v
Object--->(Object)
^ ^^
| ||
| / |
| / |
| / |
| / |
Module--->(Module)
^ / ^
| / |
|/ |
Class---->(Class)

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Trans

4/5/2005 2:15:00 PM

0

First it would appear that your repositioned diagram has an arrow
misdirected --the arrow from Class to (Object). A Class is an Object so
it should be the other way around.

Secondly, there are a number of ways to access metaclasses. Try,

class Object
class << self
# (Object)
end
end

Substitute Class, Module or OtherClass for Object. They all should
work. Other means:

class Object
def Object.a_method
# this will be a method of metaclass
end
def self.another_method
# so will this
end
end

Realize that a classes' metaclass is the same formalism as an object's
singleton class.

Lionel Thiry

4/5/2005 3:12:00 PM

0

Ilias Lazaridis a écrit :
> [snip]
> I cannot access any of the metaclasses
>
> the same in the documentation: no metaclasses
>
> -
>
> Any suggestions / clarifications?
>
> .
>

Most of the time, everything goes in ruby as if there weren't any kind of
metaclasses. For exemple, you cannot explicitely create your own metaclasses:

class MyMetaClass < Class
end

It raises the exception:
metaclass.rb:1: can't make subclass of Class (TypeError)

Instead of thinking in metaclasses, everything goes as if you were asked to
think in terms of singleton methods. (this has nothing to do with the Singleton
Pattern)

class MyClass
def method
puts "normal method"
end
def self.method
puts "singleton method or metaclass method?"
end
end

MyClass.new.method
# => Normal method

MyClass.method
# => singleton method or metaclass method?

p MyClass.singleton_methods
# => ["method"]

class Foo
def test
puts "instance method"
end
end

foo = Foo.new

foo.test
# => instance method

class << foo
def test
puts "singleton method"
end
end

# singleton methods have precedence over instance method
foo.test
# => singleton method

p foo.singleton_methods
# => ["test"]

--
Lionel Thiry

Jim Weirich

4/5/2005 4:48:00 PM

0

On Tuesday 05 April 2005 10:05 am, Austin Ziegler wrote:
> I've modified your diagram; ocObject* is an instance of otherClass,
> and the (ocObject)* metaclass is the metaclass for this instance,
> and it is a different metaclass than any other instance of
> otherClass.

Do all instances have their own metaclass? I thought metaclasses (singleton
classes) sprang into existence only when needed to have a place to put
singleton methods.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Austin Ziegler

4/5/2005 4:59:00 PM

0

On Apr 5, 2005 12:48 PM, Jim Weirich <jim@weirichhouse.org> wrote:
> On Tuesday 05 April 2005 10:05 am, Austin Ziegler wrote:
>> I've modified your diagram; ocObject* is an instance of
>> otherClass, and the (ocObject)* metaclass is the metaclass for
>> this instance, and it is a different metaclass than any other
>> instance of otherClass.
> Do all instances have their own metaclass? I thought metaclasses
> (singleton classes) sprang into existence only when needed to have
> a place to put singleton methods.

That's my understanding, too. But since you can't look at a
metaclass without instantiating it, the minor sleight of hand that I
performed above is (IMO) acceptable.

Consider:

a = Object.new; b = Object.new
puts a.id, b.id
# 22756396
# 22756384
class << a; puts self.id; end; class << b; puts self.id; end
# 22738408
# 22738396

In effect, all instances have their own metaclass. As an
implementation detail, though, they aren't instantiated until they
are needed. I'm not sure that there's a meaningful difference
between the lazy instantiation and constant instantiation from the
Ruby programmer's point of view -- unless you're doing something
really freaky with AST parsers ;)

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


ts

4/5/2005 5:09:00 PM

0

>>>>> "A" == Austin Ziegler <halostatue@gmail.com> writes:

A> I've modified your diagram; ocObject* is an instance of otherClass,
A> and the (ocObject)* metaclass is the metaclass for this instance,
A> and it is a different metaclass than any other instance of
A> otherClass.

A> ocObject*---->(ocObject)*
A> | |
A> | |
A> v v
A> otherClass---> (OtherClass)
A> | |

Well, if ocObject is an instance of otherClass then you have a small
problem

svg% cat b.rb
#!/usr/local/bin/ruby
class A
def self.a
puts "A::a"
end

def a
puts "A#a"
end
end

class << a = A.new
def a
puts "a#a"
super
end
end

a.a
svg%

svg% b.rb
a#a
A#a
svg%



Guy Decoux


Jim Weirich

4/5/2005 5:18:00 PM

0

On Tuesday 05 April 2005 08:34 am, Ilias Lazaridis wrote:
> this can be drawn like this:
>
[... ASCII art elided ...]

Umm ... I think you have the arrow between Class and (Object) pointing the
wrong direction.

I've drawn a quasi-UML diagram for the above. You can find it here:
http://onest.../images/rubyo...

I've included a singleton object as well. I'm not sure if the singleton class
gets its own metaclass or shares the same meta class with its superclass.

Comments are welcome ... I'll update it if needed.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Austin Ziegler

4/5/2005 5:19:00 PM

0

On Apr 5, 2005 1:08 PM, ts <decoux@moulon.inra.fr> wrote:
> >>>>> "A" == Austin Ziegler <halostatue@gmail.com > writes:
> A> I've modified your diagram; ocObject* is an instance of otherClass,
> A> and the (ocObject)* metaclass is the metaclass for this instance,
> A> and it is a different metaclass than any other instance of
> A> otherClass.
>
> A> ocObject*---->(ocObject)*
> A> | |
> A> | |
> A> v v
> A> otherClass---> (OtherClass)
> A> | |
> Well, if ocObject is an instance of otherClass then you have a small
> problem

You're right. I just copied a part of the diagram without thinking.

ocInstance---->(ocInstance)
|
|
v
otherClass---->(OtherClass)
| |
| |
v v
Object---->(Object)--+
^ ^ |
| | |
| | |
| | |
| | |
| | |
Module---->(Module) |
^ ^ |
| | |
| | |
Class---->(Class) |
^ |
| |
+-----------------+

I think that captures the instance case a bit better.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


ts

4/5/2005 5:26:00 PM

0

>>>>> "A" == Austin Ziegler <halostatue@gmail.com> writes:

A> ocInstance---->(ocInstance)
A> |
A> |
A> v
A> otherClass---->(OtherClass)

Well, write it like this

ocInstance-->(ocInstance)
/
/
/
/
/
/
/
/
v
otherClass-->(OtherClass)


and don't say that ruby create the "singleton" class for ocInstance, only
when it need it :-)


Guy Decoux




Ilias Lazaridis

4/7/2005 10:53:00 AM

0

Trans wrote:
> First it would appear that your repositioned diagram has an arrow
> misdirected --the arrow from Class to (Object). A Class is an Object so
> it should be the other way around.

I was not carefull whilst simplifying the diagramm.

possibly now I can understand:

otherClass--->(OtherClass)
| |
| |
v v
Object--->(Object)
^ |^
| ||
| / |
| / |
| / |
| / |
Module--->(Module)
^ / ^
| / |
|| |
|v |
Class---->(Class)

> Secondly, there are a number of ways to access metaclasses. Try,
>
> class Object
> class << self
> # (Object)
> end
> end
>
> Substitute Class, Module or OtherClass for Object. They all should
> work. Other means:
>
> class Object
> def Object.a_method
> # this will be a method of metaclass
> end
> def self.another_method
> # so will this
> end
> end
>
> Realize that a classes' metaclass is the same formalism as an object's
> singleton class.

I'm sorry.

I cannot extract any essence.

[I have reviewed all the other answer, too - but now I am even more
confused now. will give it tomorrow another try]

.

--
http://laz...