Tag: 超级

为什么’super’是关键字而不是Ruby中的方法?

在Ruby中, super是关键字而不是方法。 为什么这样设计? Ruby的设计倾向于尽可能多地实现方法; 关键字通常保留用于具有自己的语法规则的语言function。 然而, super看起来像一个方法调用。 (我知道在纯Ruby中实现super会很麻烦,因为它必须从caller解析出方法名,或者使用trace_func 。仅此一项不会阻止它成为一种方法,因为内核很多方法没有在纯Ruby中实现。)

调用super()会导致父类中的其他方法被使用吗?

我有一个关于super的问题我想要确认。 请考虑以下代码示例: class InFasionHello def hello person greet person.name end def greet name p ‘Dude, hey ‘ + name end end class OldFasionedHello < InFasionHello def hello person greet person.name if person.old_fashioned super(person) if !person.old_fashioned end def greet name p 'Good Day to you ' + name + '!' end end 我的问题是,如果我使用的是OldFasionedHello ,那么infasionHello使用本地greet自己还是来自通过super调用它的类?

如何在Ruby中重新定义Fixnum的+(plus)方法并保留原始+function?

这引发了我在1.9.2 Ruby中的SystemStackError( 但在Rubinius中工作 ): class Fixnum def +(other) self + other * 2 end end 但是没有super + (基于其他错误)。 如何访问原始+function?

构造函数重写

我上课了: class One def initialize; end end 我需要使用我自己的构造函数创建一个新类,如下所示: class Two < One def initialize(some) puts some super end end Two.new("thing") 但是当我启动代码时,我收到了一个错误: thing test.rb:10:in `initialize’: wrong number of arguments (1 for 0) (ArgumentError)