From: Alex Stahl Date: 2010-11-11T15:45:29+09:00 Subject: Re: what's an object? --=-cCYQZ+g7r1LJqFyTH56b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Method class: http://ruby-doc.org/core/classes/Method.html ________________________________________________________________________ Alex Stahl | Sr. Quality Engineer | hi5 Networks, Inc. | astahl@hi5.com | On Wed, 2010-11-10 at 23:55 -0600, timr wrote: > On Nov 10, 5:50 pm, Eva wrote: > > I'm also switching from perl and php. > > I'm not sure in ruby what's an object. > > It seems each instance of a class is an object, is it? > > > > Thanks. > > Classes are tables of defined behaviors (i.e. methods). An instance of > a class is indeed an object, as you noted. When a message is sent to > an object ("some string".upcase, "some string".reverse, "some > string".split('') etc.), the object looks inside the class that > instantiated it to see if it has the definition for that method. If > not it looks higher up the class ancestry chain to find the > definition. It may surprise you coming from perl and php that some > very basic things are objects in ruby. For instance, integers are > objects since they are instances of the Fixnum class and have an > object_id. > > Alex said everything is an object--including an instance of a class, > the class itself, its methods. I agree up until he mention methods as > objects. I don't think that can be demonstrated. For instance, we > cannot get the object_id of a method. '+.object_id' doesn't work. So, > I don't agree that methods are objects. Classes on the other hand, are > instances of the Class class, and therefore, in a brain twisting way, > are objects with object_ids. > > Demonstration: > Monkey = Class.new > > #equivalent to > #class Monkey; end > > Monkey.class # => Class > Monkey.object_id # => 2159398000 > > In addition to defining behavior, classes also can define state. The > state is stored in instance variables. These concepts, defining the > behavior and the state, are at the heart of object oriented > programming. > --=-cCYQZ+g7r1LJqFyTH56b--