[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Metaclass confusion

Vivek

1/25/2006 4:25:00 AM

Hi,
can someone explain this.

irb(main):013:0> Class.instance_methods
=> ["superclass", "new"]
irb(main):014:0> Class.class
=> Class

I guess Class is an object(an instance of Class?) but its of type
Class. How can an object's type be an object itself.?

Vivek

8 Answers

Eero Saynatkari

1/25/2006 4:53:00 AM

0

Vivek wrote:
> Hi,
> can someone explain this.
>
> irb(main):013:0> Class.instance_methods
> => ["superclass", "new"]
> irb(main):014:0> Class.class
> => Class
>
> I guess Class is an object(an instance of Class?) but its of type
> Class. How can an object's type be an object itself.?

The traditional response is 'magick' :) While it is useful to
say that all instances are Objects and Object is an instance of
class Class which itself is an Object (the class of which is Class),
at some point there has to be a limit to how deep the recursion
goes. Because ruby is implemented in C, the language design allows
this kind of 'shortcutting'.

To be productive, you sort of just have to accept this and know the
implications of Class being an Object of Class and so on. You can
don some super-chromatic peril-sensitive sunglasses if you like :)

> Vivek


E

--
Posted via http://www.ruby-....


Logan Capaldo

1/25/2006 4:57:00 AM

0


On Jan 24, 2006, at 11:28 PM, Vivek wrote:

> Hi,
> can someone explain this.
>
> irb(main):013:0> Class.instance_methods
> => ["superclass", "new"]
> irb(main):014:0> Class.class
> => Class
>
> I guess Class is an object(an instance of Class?) but its of type
> Class. How can an object's type be an object itself.?
>
> Vivek
>
>

In ruby, everything is an object. Even classes. Class inherits from
object just like everything else (well it inherits from Module which
inherits from Object). Its a little weird to get used to, but that's
how it works.


Vivek

1/25/2006 8:37:00 AM

0

One implication I have seen is that the class definition itself is an
object which can be executed.This has some interesting properties.

Robert Klemme

1/25/2006 9:51:00 AM

0

Vivek wrote:
> One implication I have seen is that the class definition itself is an
> object which can be executed.This has some interesting properties.

Umm, in usual terminology objects cannot be "executed". You can "call" or
"invoke" methods but you cannot execute an object as such. You probably
meant the right thing - it just sounded quite strange... :-)

Kind regards

robert

Sam Kong

1/25/2006 5:05:00 PM

0

Hi,

Vivek wrote:
> Hi,
> can someone explain this.
>
> irb(main):013:0> Class.instance_methods
> => ["superclass", "new"]
> irb(main):014:0> Class.class
> => Class
>
> I guess Class is an object(an instance of Class?) but its of type
> Class. How can an object's type be an object itself.?

The following doc helped me understand Ruby object model pretty well.

Understanding Ruby's Object Model [ ChrisPine_UROM.ppt ]
(http://www.ruby-doc.org/docs/Understanding%20Ruby's%20Object%20Model/ChrisPin...)
at http://www.ruby-doc.or...

Hope this helps.

Sam

Levin Alexander

1/25/2006 6:53:00 PM

0

On 1/25/06, Logan Capaldo <logancapaldo@gmail.com> wrote:> In ruby, everything is an object. Even classes. Class inherits from> object just like everything else (well it inherits from Module which> inherits from Object). Its a little weird to get used to, but that's> how it works.<http://redhanded.hobix.com/cult/lastlyTheSinkingSymphonyVideo.html...

Jules

1/25/2006 6:58:00 PM

0

A non-circular model would be prototype based inheritance. You just
clone things to subclass them:

Song = Object.new

Song.attr_writer :title
Song.title = 'No title set'

Song.define_method :play
puts "Playing #{@title}"
end

a_song = Song.clone
a_song.title = 'Song one'
a_song.play

another_song = Song.clone
another_song.title = 'Song two'

subclass_of_a_song = a_song.clone
subclass_of_a_song.play

Then there is no distinction between class/object. This is how
Javascript does it, and it seems a good solution.

But we have a class based model, not so clean, but acceptable ;-)

Jules

--
Posted via http://www.ruby-....


matthew.moss.coder

1/25/2006 8:16:00 PM

0

Heh heh, shouldn't that first line be:

Song = Object.clone

?

On 1/25/06, Jules Jacobs <julesjacobs@gmail.com> wrote:
> A non-circular model would be prototype based inheritance. You just
> clone things to subclass them:
>
> Song = Object.new
>
> Song.attr_writer :title
> Song.title = 'No title set'
>
> Song.define_method :play
> puts "Playing #{@title}"
> end
>
> a_song = Song.clone
> a_song.title = 'Song one'
> a_song.play
>
> another_song = Song.clone
> another_song.title = 'Song two'
>
> subclass_of_a_song = a_song.clone
> subclass_of_a_song.play
>
> Then there is no distinction between class/object. This is how
> Javascript does it, and it seems a good solution.
>
> But we have a class based model, not so clean, but acceptable ;-)
>
> Jules
>
> --
> Posted via http://www.ruby-....
>
>