I tried to modify Daniel Schierbeck's code (in response to the recent
"abstract class" thread) to not have an additional method dispatch.
This line here:
def self.new(*args, &block)
__new__(*args, &block)
end
cries out for a direct alias, to me. Unfortunately, what I hoped would
work didn't. It looks like #alias_method only looks for methods on the
current class; it's not able to alias to a method from an ancestor
class.
class Class
alias_method :__new__, :new
end
module NonInstantiable
def self.included(klass)
super
klass.module_eval {
def self.inherited( subklass )
subklass.module_eval {
puts "Is __new__ available? #{method( :__new__ )}"
alias_method :new, :__new__
}
end
}
end
end
class BaseKlass
include NonInstantiable
end
class SubKlass < BaseKlass; end
p SubKlass.new
p BaseKlass.new
#=> Is __new__ available? #<Method: Class#__new__>
#=> tmp.rb:12:in `alias_method': undefined method `__new__' for class
`SubKlass' (NameError)
Is this behavior desirable, a necessary evil, or an oversight?