I know there is no concept of an abstract class in Ruby. But if it needs to be implemented, how do I go about it? I tried something like this:
class A def self.new raise 'Doh! You are trying to write Java in Ruby!' 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.
Does anyone have any pointers?
I don’t like using abstract classes in Ruby (there’s almost always a better way). If you really think it’s the best technique for the situation though, you can use the following snippet to be more declarative about which methods are abstract:
Basically, you just call
abstract_methodswith the list of methods that are abstract, and when they get called by an instance of the abstract class, aNotImplementedErrorexception will be raised.