Ron Jeffries <ronjeffries / REMOVEacm.org> writes:

> I don't understand the notation above with class << etc. What's up with
> that? How is it different from
> 
> class Set
>   protected :new
> end

'new' isn't an instance method of class Set, so the line above
wouldn't work. Instead, new is defined in Set's metaclass. You can
pop into the metaclass using the singleton class thingy.

   class <<x
     ..
   end

means start defining stuff in the class specific to object 'x'. Inside
a normal class definition, 'self' is the class itself, so class <<self
gets us into the meta class. Hence

  class <<self
    protected :new
  end

> I think I get this. It's basically the same deal but avoids having to
> predeclare the subclasses up at the top, right? Definitely a better
> way.

Yes - the key is to encapsulate the type-specific factories inside the
source for the types, and not in the source for the base class.

> >This schema means you can add new concrete representations without
> >ever changing the base class.
> 
> I guess you mean without ever editing the base class's file if it
> has its own. Certainly defining a method on it is changing the class
> itself?

Picky :)


Dave