From: hochherz Date: 2005-12-02T03:57:31+09:00 Subject: Re: abstract class nice solution Daniel Schierbeck wrote: > hochherz wrote: > >> hey >> >> is there something like abstract classes in ruby? >> somehow a class i could not create but wich is a superclass of some >> classes. >> >> thanks >> olaf > > > class Class > alias_method :__new__, :new > end > > module NonInstantiable > def self.included(klass) > super > klass.module_eval do > def self.new > raise "Cannot instantiate abstract class #{self}" > end > > def self.inherited(subklass) > subklass.module_eval do > def self.new(*args, &block) > __new__(*args, &block) > end > end > end > end > end > end > > class BaseKlass > include NonInstantiable > end > > class SubKlass < BaseKlass; end > > puts SubKlass.new > puts BaseKlass.new