Group: comp.lang.ruby
From: UpsNDowns
Date: Saturday, February 16, 2008 2:30 PM
Subject: Re: object specific methods and id

Hi Gary,

Thanks a bunch! This discussion has been really helpful!

Apart from the 'direct' mistakes (like the prediction of what
b.class.superclass would return) it has finally dawned on me that indeed
the singletons are parallel.
In between my last post and your last, I read this:

http://www.chariotsolutions.com/slides/pdfs/rubyeast2007-black-PerObjectBehavior.pdf

which is attributed to some David Black and it talks about the birth
classes etc. I assume it's this David Black you mentioned :). Im new to
ruby, so who is David Black in the ruby community?

Alas, my problem now is, that an assignment for my CS class requires me
to prove that the obj is an instance of the singleton. (And no, it's no
trick question, the teacher has even lectured about it and proposed a
solution at the labs--- which btw I don't buy).

>
> OK, but the counter example is that obj.class is *not* the singleton
> class and the singleton class is not a subclass of obj.class.
>
Pretending to be the devil's advocate, this could be explained away (I
*guess*) by claiming that the singleton class could have provided a
masking implementation of the class method.

I would say this much though: The singletons are fuzzy, (the slides from
David Black ignores and belittles the importance of the implementation,
and dare I say the principles behind them). It also seems to me that the
results from various methods in ruby are inconsistent. (the
each_object(self) trick--- or perhaps it's a matter of documenting that
method)

Kind regards and thanks again.

Thomas.

Gary Wright wrote:
> On Feb 16, 2008, at 12:09 PM, UpsNDowns wrote:
>
>>My understanding of each_object is that it finds all instances of a
>>supplied class (it accepts other types of arguments). So, to me,
>>taken at face value the example supports the case that there is a
>>class/instance relationship?
>
>
> OK, but the counter example is that obj.class is *not* the singleton
> class and the singleton class is not a subclass of obj.class.
>
> What it boils down to is that Ruby's singleton mechanism introduces a
> class/instance relationship that is parallel to the standard class/
> instance relationship. If you try to describe one in terms of the
> other (or visa versa) you are going to get stuck somewhere along the
> way. I think it is better to think of them as parallel relationships
> rather than one being defined by the other.
>
> David Black talks about the 'birth class' of an object vs. the
> 'singleton class' of an object. In both cases the object is an
> 'instance' of the class' but neither relationship can be defined in
> terms of the other.
>
>
>>>>* Im not sure I understand what you mean by outside the
>>>>hierachy. My understanding is that the singleton is subclasses
>>>>the original class.
>>>
>>>class A; end
>>>class B < A; end
>>>a = A.new
>>>b = B.new
>>>S = (class <>>>a.kind_of?(A) # true
>>>a.kind_of?(S) # true, as if S were a subclass of A but...
>>>B < A # true, B is a subclass of A
>>>S < A # nil, S is not a subclass of A
>>>b.class.superclass # A
>>>B.superclass # A
>>>S.superclass # A's Singleton Class, not A
>>
>>I would believe that b.class.superclass produces something like
>>'Object' as I would expect b.class to return an instance of the
>>Class class. The Class class derives from Object.
>
>
> No.
>
> b.class # B, b is an instance of B
> b.class.superclass # A, B is a subclass of A
> b.class.superclass.superclass # Object, A is a subclass of Object
>
>
>>puts objSingletonClass.superclass
>>#
>
>
>
> True, but '#' is simply a chuck of text returned by the
> #to_s method. Singleton classes come into existence when they are
> referenced and unless you explicitly assign them to a constant they
> will by anonymous (as in they will not have a string associated with
> their Class#name attribute). #to_s (and #inspect) actually ignores
> that name assignment though and just crafts its own output:
>
> x = Object.new
>
> xs = (class <>
> puts xs.name # => ""
> puts xs.to_s # => "#>"
> puts xs.inspect # => "#>"
>
> XS = xs
> puts xs.name # => "XS"
> puts xs.to_s # => "#>"
> puts xs.inspect # => "#>"
>
> xs_super = xs.superclass
> puts xs_super.name # => ""
> puts xs_super.to_s # => "#"
> puts xs_super.inspect # => "#"
>
> os = (class <> puts os.name # => ""
> puts os.inspect # => "#"
>
> XSS = xs_super
> puts xs_super.name # => "XSS"
> puts xs_super.to_s # => "#"
> puts xs_super.inspect # => "#"
>
>

Safety Articles | Usenet Groups | Usenet News | Bluegrass