How to implement an abstract class in ruby?

Posted by Chirantan on Stack Overflow See other posts from Stack Overflow or by Chirantan
Published on 2009-02-04T17:35:43Z Indexed on 2010/03/24 13:03 UTC
Read the original article Hit count: 219

Filed under:
|

I know there is no concept of abstract class in ruby. But if at all it needs to be implemented, how to go about it? I tried something like...

class A
  def self.new
    raise 'Doh! You are trying to instantiate an abstract class!'
  end
end

class B < A
  ...
  ...
end

But when I try to instantiate B, it is internally going to call A.new which is going to raise the exception.

Also, modules cannot be instantiated but they cannot be inherited too. making the new method private will also not work. Any pointers?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about abstract-class