On Nov 10, 5:50=A0pm, Eva <eva54... / sina.com> 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 =3D Class.new

#equivalent to
#class Monkey; end

Monkey.class # =3D> Class
Monkey.object_id # =3D> 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.