Array is an instance of class Class

If the title to this post seems impossible to understand, please read this conversation between Gavin Sinclair and Martin de Mello. Something that was really complicated turned out to be quite logical in the end. You might have to read it several times though!

Gavin Sinclair wrote:

> But I don’t see why I should juggle Ruby meta-class models in my head
> just to define class methods.
>
> One advantage of the apparently beautiful “class << self” is that you > can define several class methods at once without explicitly typing the
> class name, but I don’t see what’s gained here. To me it’s
> obfuscated, and even when understood – or merely acknowledged – I
> think the notation is unintuitive.
>
> Hopefully I’m missing something and am about to have my fourth Ruby
> epiphany 🙂

It seems consistent to me. If you wanted to define several methods for
class Array, you’d write

class Array
[define methods here]
end

and all instances of Array can now use your methods
similarly, if you want to define several methods for an object’s singleton
class, you write

class << object
[define methods here]
end

and all instances of object’s singleton class (i.e., just object) can
now access the methods.

As for class methods – well, a class is just an instance of class Class.
Therefore, it has methods which are instance methods of Class. You can
add in methods by saying

class Class
[define methods here]
end

and all classes will now have the new class methods. If you want class
methods for a single class, you are actually tacking on methods to an
object, in much the same way as “def a.newmethod” would. So you can use
that syntax:

def Array.newmethod1
[define]
end

or you can say

class << Array
[define]
end

or, if you are already *in* class Array and defining instance methods,
you can highlight the fact that you’re working in the same conceptual
space by taking advantage of Ruby’s setting ‘self’ to ‘Array’ when you say
‘class Array’, and writing

class Array
def instancemethod
[define]
end
class << self
def classmethod
[define]
end
end
end

There’s no arcane magic going on here – self is an object of class
Class, and in this case it has the value Array.

Try the following:

class Array
p self
p self.class
p self.id
end

sss

class << Array
p self
p self.class
p self.id
end

sss

class Array
class << self
p self
p self.class
p self.id
end
end

sss

a = Array.new
class << a
p self
p self.class
p self.id
end

and, to prove that singleton classes are being created, even though they
all say Array,

b = Array.new
class << b
p self
p self.id
end

p Array.id

Martin de Mello
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/57252

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.