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