Ruby-在超类中调用另一个Class方法
|
class A
def self.a
puts \"self: #{self}; superclass: #{superclass}\"
end
end
class B < A
class << self; undef_method(:a); end # I\'m not allowed to use B.a
def self.b
# here I want to call A.a with B as self.
end
end
A.a #=> self: A; superclass: Object
B.b #=> self: B; superclass: A (expected)
我不想要alias_method
解决方案。我正在寻找这样的东西。
更新
该解决方案无需与上面的链接相似;这只是一个建议。例如,我尝试做:
class B < A
def self.b
instance_eval(&A.method(:a).to_proc)
end
end
但是这样我在Ruby 1.8.7上得到了一个奇怪的ArgumentError
。
没有找到相关结果
已邀请:
3 个回复
暑袜眠退
是单例方法。单例方法只能绑定到使用其创建的对象。特别是不能反弹至
。 这是我尝试的第一种无效方法:
第二种无效方法:
两者都生成一个\“ TypeError:绑定到另一个对象的单例方法\”异常。
括宠
好按紊叉
。