rbaldwin wrote:
> On Dec 12, 5:10 pm, "Tim Rowe" <n... / digitig.co.uk> wrote:
>> What's the Ruby way to define a class that shouldn't be instantiated, but
>> with descendants that can be? Other than a comment in the documentation, of
>> course.
>>
>> Thanks!
> 
> Here is one way. Its not very satisfying, as you have no way to access
> the initialize method on any parent classes of the Animal class (such
> as ActiveRecord)...

You could privatize new and define a new new for inheriting classes that 
calls the old new:

class Animal
   class << self
     private :new
     def inherited(cls)
       cls.instance_eval "def new(*args); super; end"
     end
   end
end

class Dog < Animal

puts Dog.new # => #<Dog:0x2506c>
pust Animal.new => NoMethodError: private method 'new' called for Animal...

I use instance_eval here because I don't like the semantics of 
define_method (or I dislike them more than eval magic); but it would 
probably work about the same.

The downside of this approach is subclasses that want to also define 
their own 'new' would have to know to call super. I'd expect that's 
pretty rare.

- Charlie