What's an object?

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.

Simple answer: everything. Everything is considered an object,
including an instance of a class, the class itself, its methods, etc.
No primitives, just objects.

···

________________________________________________________________________

Alex Stahl | Sr. Quality Engineer | hi5 Networks, Inc. | astahl@hi5.com

On Wed, 2010-11-10 at 19:50 -0600, 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.

···

On Nov 10, 5:50 pm, 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.

One other point. A local variable is not an object and never is -
although you cam get an object which represents the stack frame it lives
with (a Binding)

···

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

The first question is: "What is an object?"

Ruby Draft Specification answers the question, also. Sec.6.1 on the
document says:

···

---
6.1 Objects

An object has states and a behavior. An object has a set of bindings
of instance variables (see 6.2.2) as one of its states. Besides the
set of bindings of instance variables, an object can have some
attributes as its states, depending on the class of the object. The
behavior of an object is defined by a set of methods (see 6.3) which
can be invoked on that object. A method is defined in a class, a
singleton class, or a module (see 6.5).

Every value directly manipulated by a program is an object. For
example, all of the following values are objects:

-ˆ A value which is referred to by a variable (see 6.2);
-ˆ A value which is passed to a method as an argument;
ˆ - A value which is returned by a method;
-ˆ A value which is returned as the result of evaluating an
expression (see Clause 11), a statement (see Clause 12), a
compound-statement (see 10.2), or a program (see 10.1).

Other values are not objects, unless explicitly specified as objects.
---

Regards,

2010/11/11 Eva <eva54321@sina.com>:

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.

--
NOBUOKA Yuya

Simple answer: everything. Everything is considered an object,
including an instance of a class, the class itself, its methods, etc.

There is one thing incorrect.
In Ruby, an instance of a class or a class itself is exactly an object,
while a method is NOT an object.

Although we can obtain a Method object using the *Object#method*
method [ class Object - RDoc Documentation ], a
method itself is not an object.

···

--
NOBUOKA Yuya

Simple answer: everything. Everything is considered an object,
including an instance of a class, the class itself, its methods, etc.

class itself is the instance of Class class, so a class is exactly an object.

Class.class

=> Class

But Class is the instance of itself, I can't understand for this. who
can help explain?

···

2010/11/11 Alex Stahl <astahl@hi5.com>:

--
Kind regards,
Zuer (祖儿)

Method class: class Method - RDoc Documentation

···

________________________________________________________________________

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 <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 = 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.

> Simple answer: everything. Everything is considered an object,
> including an instance of a class, the class itself, its methods, etc.

There is one thing incorrect.
In Ruby, an instance of a class or a class itself is exactly an object,
while a method is NOT an object.

Although we can obtain a Method object using the *Object#method*
method [ class Object - RDoc Documentation ], a
method itself is not an object.

This seems self-contradictory, how is a method not an object?

···

On Wed, Nov 10, 2010 at 10:25 PM, Y. NOBUOKA <nobuoka@r-definition.com>wrote:

On Wed, Nov 10, 2010 at 11:55 PM, timr <timrandg@gmail.com> wrote:

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.

1.method('+').object_id

One other point. A local variable is not an object and never is -

I think that's too general and confusing, at least to me.

def m
  a = [1,2,3]
  p a.is_a?(Object)
  end

=> nil

m

true
=> true

Is there more insight behind that statement? Or am I missing something
fundamental?

Thanks,
Ammar

···

On Thu, Nov 11, 2010 at 10:51 AM, Brian Candler <b.candler@pobox.com> wrote:

No variable ever *is* an object in Ruby. Variables *reference* objects!

Kind regards

robert

···

On Thu, Nov 11, 2010 at 9:51 AM, Brian Candler <b.candler@pobox.com> wrote:

One other point. A local variable is not an object and never is -
although you cam get an object which represents the stack frame it lives
with (a Binding)

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Not that I know the internals of the language well enough to debate the
internal representation of a method, everything I've read suggests that,
at a conceptual level, they can be regarded as such. Otherwise, how
would we reconcile this description from the Method class link?
(http://ruby-doc.org/core/classes/Method.html\)

meth == other_meth => true or false
"Two method objects are equal if that are bound to the same object and
contain the same body"

Couldn't methods be considered specialized proc objects bound to a
particular instantiation of an object? Also, there's this description
at the link you provided:

obj.method(sym) => method
"Looks up the named method as a receiver in obj, returning a Method
object (or raising NameError). The Method object acts as a closure in
obj‘s object instance, so instance variables and the value of self
remain available."

···

________________________________________________________________________

Alex Stahl | Sr. Quality Engineer | hi5 Networks, Inc. | astahl@hi5.com

On Wed, 2010-11-10 at 22:25 -0600, Y. NOBUOKA wrote:

> Simple answer: everything. Everything is considered an object,
> including an instance of a class, the class itself, its methods, etc.

There is one thing incorrect.
In Ruby, an instance of a class or a class itself is exactly an object,
while a method is NOT an object.

Although we can obtain a Method object using the *Object#method*
method [ class Object - RDoc Documentation ], a
method itself is not an object.

I suggest a modification of Alex's definition:

Everything /which can be referenced through a variable/ is considered an object.

This includes classes, instances of Method etc. It's just that Method
and subclasses are basically only proxies to the underlying
implementation of a method - with limited functionality (obtaining
arity and executing them for example). The mere fact that we obtain a
new instance whenever we invoke #method hints at this:

irb(main):005:0> s=""; 2.times.map { s.method(:length).object_id }
=> [134981756, 134981742]

Kind regards

robert

···

On Thu, Nov 11, 2010 at 5:25 AM, Y. NOBUOKA <nobuoka@r-definition.com> wrote:

Simple answer: everything. Everything is considered an object,
including an instance of a class, the class itself, its methods, etc.

There is one thing incorrect.
In Ruby, an instance of a class or a class itself is exactly an object,
while a method is NOT an object.

Although we can obtain a Method object using the *Object#method*
method [ class Object - RDoc Documentation ], a
method itself is not an object.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I assume he means a itself, as opposed to the object that a is referencing.

I personally think that most of these conversations end up pedantic and
useless. I subscribe to Yehuda Katz's view that if a complicated mental
model doesn't provide anything useful, it should not be used as an
explanation

···

On Thu, Nov 11, 2010 at 2:59 AM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Thu, Nov 11, 2010 at 10:51 AM, Brian Candler <b.candler@pobox.com> > wrote:
> One other point. A local variable is not an object and never is -

I think that's too general and confusing, at least to me.

>> def m
>> a = [1,2,3]
>> p a.is_a?(Object)
>> end
=> nil
>> m
true
=> true

Is there more insight behind that statement? Or am I missing something
fundamental?

Thanks,
Ammar

Now that's a different, or at least clearer, statement. And it fits
with what I know.

Thanks,
Ammar

···

On Thu, Nov 11, 2010 at 11:27 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Thu, Nov 11, 2010 at 9:51 AM, Brian Candler <b.candler@pobox.com> wrote:

One other point. A local variable is not an object and never is -
although you cam get an object which represents the stack frame it lives
with (a Binding)

No variable ever *is* an object in Ruby. Variables *reference* objects!

> One other point. A local variable is not an object and never is -

I think that's too general and confusing, at least to me.

>> def m
>> a = [1,2,3]
>> p a.is_a?(Object)
>> end
=> nil
>> m

true
=> true

Is there more insight behind that statement? Or am I missing something
fundamental?

Thanks,
Ammar

When you ask object#is.a?(Class) you are asking if the Class is part
of the ancestry of the object. So you ask an array if it inherits
directly or indirectly from the Object class, and the answer is yes.
Here is a demo:

class New < String
end

=> nil

n = New.new

=> ""

n.is_a? New

=> true

n.is_a? String

=> true

n.is_a? Array

=> false

n.is_a? Object

=> true

New.ancestors

=> [New, String, Enumerable, Comparable, Object, Kernel]

But there is a distinction between inheriting from the Object class
and being an object. A better demonstration that the object like the
variable a points to in your example, is an instantiated object is to
ask it if it has an object_id--and it will. So the answer agrees with
the is_a? result, but the questions are asking very different things.
Tim

···

On Nov 11, 12:59 am, Ammar Ali <ammarabu...@gmail.com> wrote:

On Thu, Nov 11, 2010 at 10:51 AM, Brian Candler <b.cand...@pobox.com> wrote:

Hi Ammar,
I do think the code means something different than you are
interpreting it to mean.

obj.is_a?(Object) asks if Object is listed in the ancestors of obj. It
is another way of saying obj.class.ancestors.include?(Object)

Everything you can send a message to in ruby is a descendent of
Object, so it will always be true, er, I predict. However, when you
ask a local variable (which is really a pointer to, in your case, an
instance of Array) you are asking is Object listed in the ancestors of
the Array class? It doesn't ask if a variable considers itself an
instantiated object.
Tim

···

On Nov 11, 12:59 am, Ammar Ali <ammarabu...@gmail.com> wrote:

On Thu, Nov 11, 2010 at 10:51 AM, Brian Candler <b.cand...@pobox.com> wrote:
> One other point. A local variable is not an object and never is -

I think that's too general and confusing, at least to me.

>> def m
>> a = [1,2,3]
>> p a.is_a?(Object)
>> end
=> nil
>> m

true
=> true

Is there more insight behind that statement? Or am I missing something
fundamental?

Thanks,
Ammar

>
> 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.
>
1.method('+').object_id

1.method('+') is not the same as 1.+, so your example does not prove + is
a method. The object ID you get with 1.method('+').object_id is actually
the object ID of the object that is returned by the method "method" when
it is passed an argument of the string "+". That is not the same thing
as an object ID for the + method itself.

Here's a thought experiment for you:

If + is an object, and 1.method('+') returns that object so that you can
get its object ID with 1.method('+').object_id, this should work:

    > foo = 1.method('+')
    > foo(3)
    4
    >

Fire up irb and try it, now. It doesn't work.

Instead, if you want to use it, you need to do this:

    > foo = 1.method('+')
    > foo.call(3)
    4
    >

That's because a "method object" is not a method; it is a proc that wraps
the method and its scope context.

···

On Thu, Nov 11, 2010 at 04:51:46PM +0900, Josh Cheek wrote:

On Wed, Nov 10, 2010 at 11:55 PM, timr <timrandg@gmail.com> wrote:

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Not that I know the internals of the language well enough to debate the
internal representation of a method, everything I've read suggests that,
at a conceptual level, they can be regarded as such. Otherwise, how
would we reconcile this description from the Method class link?
(http://ruby-doc.org/core/classes/Method.html\)

meth == other_meth => true or false
"Two method objects are equal if that are bound to the same object and
contain the same body"

Couldn't methods be considered specialized proc objects bound to a
particular instantiation of an object?

A Method object (= an instance of the Method class) is an object. But
it is not a method. It can be considered as a wrapper of the method
which is bound to an object.

We should think that a Method object is a thing that is different from
a method.

Please see also:

···

---
Methods are a fundamental part of Ruby's syntax, but they are not
values that Ruby programs can operate on. That is, Ruby's methods are
not objects in the way that strings, numbers, and arrays are. It is
possible, however, to obtain a Method object that represents a given
method, and we can invoke methods indirectly through Method objects.
--- From "The Ruby Programming Language" [
http://www.amazon.com/dp/0596516177/ ]

--
NOBUOKA Yuya

I assume he means a itself, as opposed to the object that a is referencing.

I had a hunch, but that statement was too general and open to misinterpretation.

I personally think that most of these conversations end up pedantic and
useless.

Even though I find some of these discussions useful to read, I agree
that most end up pedantic and of little practical use for most.

Regards,
Ammar

···

On Thu, Nov 11, 2010 at 11:20 AM, Josh Cheek <josh.cheek@gmail.com> wrote: