RubyFlow The Ruby and Rails community linklog

gem install 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

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in