From: Matthias Lampert Date: 2001-08-30T17:55:20+09:00 Subject: [ruby-talk:20571] To those who want abstract classes in Ruby Hi, folks! There are people who miss a feature like abstract/virtual classes in Ruby. I was one of them, I admit. By the time I learned, though, that I could implement this feature myself. Just like here. ######################################################################### class Abstract attr :attrib def initialize( i_attrib ) @attrib = i_attrib check4ForbiddenInstantiating end def method_a crash4ViolationOfAbstractness("method_a") end def method_b crash4ViolationOfAbstractness("method_b") end def crash4ViolationOfAbstractness( sMethodName ) raise "Ahem... You need to implement " + "#{self.type.to_s}##{sMethodName} to use it!" end def check4ForbiddenInstantiating if self.type.to_s == "Abstract" raise "Sorry, lad! I'm abstract!" end end end class Concrete < Abstract def initialize( i_attrib ) super(i_attrib) end def method_a puts "This instantiation is " + attrib.to_s puts "Implemented Abstract#method_a" end end # Abstract.new( "forbidden!") # ==> ERROR! obj = Concrete.new("allowed!") obj.method_a # obj.method_b # ==> ERROR! ######################################################################### It's primitive, I know that, but it's a place to get started, and it did a great job for me so far. Maybe one of the gurus can make a module of it; would be great to create an abstract class semi-automatically. Perhaps I can do it myself, but I need to be a guru first! CU, -- Matthias Lampert, Hamburg Just another XP and Ruby fan