Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what's an object?

Reply
Thread Tools

what's an object?

 
 
Eva
Guest
Posts: n/a
 
      11-11-2010
SSdtIGFsc28gc3dpdGNoaW5nIGZyb20gcGVybCBhbmQgcGhwLg pJJ20gbm90IHN1cmUgaW4gcnVi
eSB3aGF0J3MgYW4gb2JqZWN0LgpJdCBzZWVtcyBlYWNoIGluc3 RhbmNlIG9mIGEgY2xhc3MgaXMg
YW4gb2JqZWN0LCBpcyBpdD8KClRoYW5rcy4K


 
Reply With Quote
 
 
 
 
Alex Stahl
Guest
Posts: n/a
 
      11-11-2010
[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. | http://www.velocityreviews.com/forums/(E-Mail Removed)
|

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.


 
Reply With Quote
 
 
 
 
Y. NOBUOKA
Guest
Posts: n/a
 
      11-11-2010
> 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

 
Reply With Quote
 
zuerrong
Guest
Posts: n/a
 
      11-11-2010
2010/11/11 Alex Stahl <(E-Mail Removed)>:
> 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)

 
Reply With Quote
 
timr
Guest
Posts: n/a
 
      11-11-2010
On Nov 10, 5:50*pm, Eva <(E-Mail Removed)> 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.
 
Reply With Quote
 
Alex Stahl
Guest
Posts: n/a
 
      11-11-2010
[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. | (E-Mail Removed)
|

On Wed, 2010-11-10 at 23:55 -0600, timr wrote:

> On Nov 10, 5:50 pm, Eva <(E-Mail Removed)> 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.
>


 
Reply With Quote
 
Alex Stahl
Guest
Posts: n/a
 
      11-11-2010
[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. | (E-Mail Removed)
|

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


 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      11-11-2010
[Note: parts of this message were removed to make it a legal post.]

On Wed, Nov 10, 2010 at 10:25 PM, Y. NOBUOKA <(E-Mail Removed)>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 <(E-Mail Removed)> 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

 
Reply With Quote
 
Y. NOBUOKA
Guest
Posts: n/a
 
      11-11-2010
> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-11-2010
On Thu, Nov 11, 2010 at 5:25 AM, Y. NOBUOKA <(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments