Forum: Ruby Metaclass confusion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek (Guest)
on 2006-01-25 05:29
(Received via mailing list)
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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2006-01-25 05:53
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-01-25 05:59
(Received via mailing list)
On Jan 24, 2006, at 11:28 PM, Vivek wrote:

>
> 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.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek (Guest)
on 2006-01-25 09:41
(Received via mailing list)
One implication I have seen is that the class definition itself is an
object which can be executed.This has some interesting properties.
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-25 10:53
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-25 15:13
(Received via mailing list)
Hi --

On Wed, 25 Jan 2006, Eero Saynatkari wrote:

>> Class. How can an object's type be an object itself.?
> don some super-chromatic peril-sensitive sunglasses if you like :)
I don't think any peril is involved (though lately all discussions
seem to lead to talking about shooting in foot, running with scissors,
etc. :-)  It's just that there's some circularity at the top of the
class/module hierarchy, for the sake of bootstrapping the whole
system.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Ca0b18ec9e11dc777b2b8084fe5d5f90?d=identicon&s=25 Sam Kong (Guest)
on 2006-01-25 18:09
(Received via mailing list)
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'...)
at http://www.ruby-doc.org/whyruby/

Hope this helps.

Sam
0d298cda3121e5cacaa2465437769025?d=identicon&s=25 Levin Alexander (Guest)
on 2006-01-25 19:55
(Received via mailing list)
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/lastlyTheSinkingSy...

-Levin
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-25 19:57
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
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-01-25 21:17
(Received via mailing list)
Heh heh, shouldn't that first line be:

Song = Object.clone

?
This topic is locked and can not be replied to.