Group: comp.lang.ruby
From: Gary Wright
Date: Saturday, February 16, 2008 1:37 PM
Subject: Re: object specific methods and id


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