Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   what's an object? (http://www.velocityreviews.com/forums/t865200-whats-an-object.html)

Eva 11-11-2010 01:50 AM

what's an object?
 
SSdtIGFsc28gc3dpdGNoaW5nIGZyb20gcGVybCBhbmQgcGhwLg pJJ20gbm90IHN1cmUgaW4gcnVi
eSB3aGF0J3MgYW4gb2JqZWN0LgpJdCBzZWVtcyBlYWNoIGluc3 RhbmNlIG9mIGEgY2xhc3MgaXMg
YW4gb2JqZWN0LCBpcyBpdD8KClRoYW5rcy4K



Alex Stahl 11-11-2010 01:57 AM

Re: what's an object?
 
[Note: parts of this message were removed to make it a legal post.]

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.



Y. NOBUOKA 11-11-2010 04:25 AM

Re: what's an object?
 
> 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 [ http://ruby-doc.org/core/classes/Object.html#M000336 ], a
method itself is not an object.

--
NOBUOKA Yuya


zuerrong 11-11-2010 05:23 AM

Re: what's an object?
 
2010/11/11 Alex Stahl <astahl@hi5.com>:
> Simple answer: =C2=A0everything. =C2=A0Everything 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 objec=
t.

> Class.class

=3D> Class

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


--=20
Kind regards,
=C2=A0 =C2=A0 =C2=A0Zuer (=E7=A5=96=E5=84=BF)


timr 11-11-2010 05:51 AM

Re: what's an object?
 
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.

Alex Stahl 11-11-2010 06:45 AM

Re: what's an object?
 
[Note: parts of this message were removed to make it a legal post.]

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



Alex Stahl 11-11-2010 06:55 AM

Re: what's an object?
 
[Note: parts of this message were removed to make it a legal post.]

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 [ http://ruby-doc.org/core/classes/Object.html#M000336 ], a
> method itself is not an object.
>



Josh Cheek 11-11-2010 07:51 AM

Re: what's an object?
 
[Note: parts of this message were removed to make it a legal post.]

On Wed, Nov 10, 2010 at 10:25 PM, 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 [ http://ruby-doc.org/core/classes/Object.html#M000336 ], a
> method itself is not an object.
>
>

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

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


Y. NOBUOKA 11-11-2010 07:51 AM

Re: what's an object?
 
> 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. =A0Otherwise, how
> would we reconcile this description from the Method class link?
> (http://ruby-doc.org/core/classes/Method.html)
>
> meth =3D=3D other_meth =3D> 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 (=3D 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:
http://stackoverflow.com/questions/2...objects-or-not

---
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/ ]

--=20
NOBUOKA Yuya


Robert Klemme 11-11-2010 08:35 AM

Re: what's an object?
 
On Thu, Nov 11, 2010 at 5:25 AM, Y. NOBUOKA <nobuoka@r-definition.com> wrot=
e:
>> Simple answer: =A0everything. =A0Everything 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 [ http://ruby-doc.org/core/classes/Object.html#M000336 ], 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 ob=
ject.

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=3D""; 2.times.map { s.method(:length).object_id }
=3D> [134981756, 134981742]

Kind regards

robert

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



All times are GMT. The time now is 09:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.