gem install abstract_class
abstract classes in ruby https://github.com/shuber/abstract_class
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
abstract classes in ruby https://github.com/shuber/abstract_class
Comments
Why? I don’t get it. Seems like unnecessary indirection. Why not just raise in the abstract class’s initializer?
class AbstractBase def initialize raise "Abstract class #{self.class} can't be allocated" if self.class == AbstractBase end end
class ConcreteInstance < AbstractBase def initialize super end end
puts “Instantiating ConcreteInstance…” c = ConcreteInstance.new puts “Instantiating AbstractBase…” a = AbstractBase.new #=> RuntimeError: Abstract class AbstractBase can’t be allocated</code>
Post a comment