Neb*_*Fox 6 ruby inheritance super ruby-1.9.3
我有一个像这样的对象
class SomeObject
def initialize &block
# do something
end
end
class AnotherObject < SomeObject
def initalize &block
super
# do something with block
end
end
Run Code Online (Sandbox Code Playgroud)
当super调用时AnotherObject,块似乎被传递给SomeObject.这是正确的行为吗?
根据rubyspec,这是正确的行为,即使你将显式参数传递给super(即super('foo'))
如果你不想传递那个块,你可以传递一个什么都不做的块,虽然这不是一回事(例如,如果方法改变了它的行为block_given?)
看起来
super(&nil)
Run Code Online (Sandbox Code Playgroud)
虽然我在红宝石规范中找不到这个,但是这是一种完全不通过块的方法.