Blocks and objects

I have such an object

class SomeObject def initialize &block # do something end end class AnotherObject < SomeObject def initalize &block super # do something with block end end 

When super is called in AnotherObject , the block seems to be passed to SomeObject . Is this the right behavior and is there around?

+6
source share
1 answer

According to rubyspec, this is the correct behavior, even if you pass explicit super arguments (i.e. super('foo') )

If you do not want to pass this block, you can simply pass a block that does nothing, although this is not quite the same (for example, if the method changes its behavior based on block_given? )

It seems that

 super(&nil) 

is a way to pass no block at all super, although I could not find it in the ruby ​​specification.

+8
source

Source: https://habr.com/ru/post/907409/


All Articles