[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

object specific methods and id

UpsNDowns

2/14/2008 10:59:00 PM

Hi,

If I create an object specifc method foo on my string instance obj, a
new anonymous class (the singleton, I gather) is created and obj is
considered an instance of that.

Also, every thing is an object (event classes). So how come,
obj.class.id
and
"someString".class.id
is the same?

According to the documentation on id: "The same number will be returned
on all calls to id for a given object, and no two active objects will
share an id."

Kind regards,

Thomas
23 Answers

Phrogz

2/14/2008 11:14:00 PM

0

On Feb 14, 3:59 pm, UpsNDowns <tnospamho...@povtal.org> wrote:
> If I create an object specifc method foo on my string instance obj, a
> new anonymous class (the singleton, I gather) is created and obj is
> considered an instance of that.
>
> Also, every thing is an object (event classes). So how come,
> obj.class.id
> and
> "someString".class.id
> is the same?

You should be using object_id; but that won't change what you see.


> According to the documentation on id: "The same number will be returned
> on all calls to id for a given object, and no two active objects will
> share an id."

Creating an eigenclass (singleton class) for an object does not change
the .class of that object.

irb(main):001:0> s1 = "hello"
=> "hello"
irb(main):002:0> s2 = "world"
=> "world"
irb(main):003:0> def s2.special_method; end
=> nil
irb(main):004:0> s1.class == s2.class
=> true
irb(main):005:0> s1.class
=> String
irb(main):006:0> s2.class
=> String

irb(main):008:0> s2eigenclass = class << s2; self; end
=> #<Class:#<String:0x2b813c8>>

irb(main):012:0> s2.class.instance_methods.grep /special/
=> []

irb(main):013:0> s2eigenclass.instance_methods.grep /special/
=> ["special_method"]

7stud --

2/14/2008 11:20:00 PM

0

UpsNDowns wrote:
> Hi,
>
> If I create an object specifc method foo on my string instance obj, a
> new anonymous class (the singleton, I gather) is created and obj is
> considered an instance of that.
>
> Also, every thing is an object (event classes). So how come,
> obj.class.id
> and
> "someString".class.id
> is the same?
>

They aren't:

puts String.class
puts 'hello'.class

--output:--
Class
String

puts String.class.object_id
puts 'hello'.class.object_id

--output:--
110210
105460
--
Posted via http://www.ruby-....

Gary Wright

2/14/2008 11:43:00 PM

0


On Feb 14, 2008, at 6:19 PM, 7stud -- wrote:
> UpsNDowns wrote:
>> If I create an object specifc method foo on my string instance obj, a
>> new anonymous class (the singleton, I gather) is created and obj is
>> considered an instance of that.
>>
>> Also, every thing is an object (event classes). So how come,
>> obj.class.id
>> and
>> "someString".class.id
>> is the same?
>>
>> They aren't:
>
> puts String.class
> puts 'hello'.class

The original poster was talking about a string instance
that has been specialized with a singleton method, not
the String class object.

UpnsNDowns: You have to be careful about thinking of the object
as an instance of its eigenclass. It is a useful analogy but it
is just that, an analogy.

Gary Wright

7stud --

2/15/2008 12:09:00 AM

0

UpsNDowns wrote:
> Hi,
>
> If I create an object specifc method foo on my string instance obj, a
> new anonymous class (the singleton, I gather) is created and obj is
> considered an instance of that.
>

According to pickaxe2, that is correct. However, the anonymous class is
called an 'anonymous' for a reason: it has no name. So, what would
obj.class return if that were to retrieve the name of the anonymous
class? nil? I would imagine the anonymous class does not have a .class
attribute, so the lookup for .class proceeds up the inheritance chain,
something like this:


class Dog
attr_accessor :name

def initialize
@name = "Rover"
end

end

#-------------------

class IntermediateClass < Dog
end

#-------------------

class Puppy < IntermediateClass
end

p = Puppy.new
puts p.name

--output:--
Rover
--
Posted via http://www.ruby-....

Phrogz

2/15/2008 12:17:00 AM

0

On Feb 14, 5:08 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
> According to pickaxe2, that is correct. However, the anonymous class is
> called an 'anonymous' for a reason: it has no name. So, what would
> obj.class return if that were to retrieve the name of the anonymous
> class?

It would return what it does:
irb(main):001:0> class << "foo"; self; end
=> #<Class:#<String:0x2b82264>>

The reason .class doesn't return the eigenclass has nothing to do with
it being 'hard' to represent. It's because it's still not the class of
the object. It's not a module, either, though it also behaves like
one. It is its own thing.

Phrogz

2/15/2008 2:52:00 AM

0

On Feb 14, 5:08 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
> I would imagine the anonymous class does not have a .class
> attribute...

Also not true:

irb(main):002:0> class << "foo"; self; end.class
=> Class

The class is, unsurprisingly, an instance of Class.

7stud --

2/15/2008 7:49:00 AM

0

Gavin Kistner wrote:

> The reason .class doesn't return the eigenclass has nothing to do with
> it being 'hard' to represent.

Who said anything about it being 'hard'?

> On Feb 14, 5:08 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
>> According to pickaxe2, that is correct. However, the anonymous class is
>> called an 'anonymous' for a reason: it has no name. So, what would
>> obj.class return if that were to retrieve the name of the anonymous
>> class?
>
> It would return what it does:
> irb(main):001:0> class << "foo"; self; end
> => #<Class:#<String:0x2b82264>>
>

...which isn't that output. Maybe you shoot for accuracy rather than
quantity.


Gavin Kistner wrote:
> On Feb 14, 5:08�pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
>>�I would imagine the anonymous class does not have a .class
>> attribute...
>
> Also not true:
>
> irb(main):002:0> class << "foo"; self; end.class
> => Class
>
> The class is, unsurprisingly, an instance of Class.

Thanks for your insightful post.

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

UpsNDowns

2/15/2008 7:12:00 PM

0

Thanks for the reply.

Gary Wright wrote:
>
> UpnsNDowns: You have to be careful about thinking of the object
> as an instance of its eigenclass. It is a useful analogy but it
> is just that, an analogy.


Oh. I was reading the book by the programmatic programmers and I believe
it says quite clearly that the object becomes an instance of the
singleton. Anyway, do you have a reference (pref web) for the analogy?

Kind regards

Gary Wright

2/16/2008 4:04:00 AM

0


On Feb 15, 2008, at 2:14 PM, UpsNDowns wrote:

> Thanks for the reply.
>
> Gary Wright wrote:
>> UpnsNDowns: You have to be careful about thinking of the object
>> as an instance of its eigenclass. It is a useful analogy but it
>> is just that, an analogy.
>
> Oh. I was reading the book by the programmatic programmers and I
> believe it says quite clearly that the object becomes an instance
> of the singleton. Anyway, do you have a reference (pref web) for
> the analogy?

Nothing comes to mind. This thread pointed out that if it was
*really* an instance of the singleton class then instance.class would
return the singleton class, but it doesn't. Another difference is
that the singleton class and its superclass are outside the class
hierarchy that created the instance in the first place. Another
difference is that the instance exists before its singleton class,
which is the opposite of the normal class/instance relationship.

These are the types of things I was getting at when I said that the
analogy was imperfect.

Gary Wright

UpsNDowns

2/16/2008 3:50:00 PM

0

Gary Wright wrote:
>
> Nothing comes to mind. This thread pointed out that if it was
> *really* an instance of the singleton class then instance.class would
> return the singleton class, but it doesn't. Another difference is
> that the singleton class and its superclass are outside the class
> hierarchy that created the instance in the first place. Another
> difference is that the instance exists before its singleton class,
> which is the opposite of the normal class/instance relationship.
>
* instance.class is a method like any other so it can be overriden by
the singleton, I think, so it can return whatever it wants. Indeed if I
from the singleton class does ObjectSpace.each_object (self) {|x| puts
x} only my instance is printed. (I suspect ObjectSpace gets help from
the VM/runtime).

* Im not sure I understand what you mean by outside the hierachy. My
understanding is that the singleton is subclasses the original class.

* As for the instance existing before the class, yeah I see that.

Kind regards