How to prevent `super` from yielding

  • A+
Category:Languages

I have a class Duck with an initialize method that yields to a block:

class Duck   def initialize()     if block_given?       yield(self)     end   end end 

and a class TalkingDuck that greets the programmer when it is initialized.

class TalkingDuck < Duck   def initialize()     super()     puts 'I am a duck'   end end 

When I call the constructor TalkingDuck.new with a block, I don't want this block to be executed. This:

TalkingDuck.new { puts 'Quack' } 

should only print I am a duck, but not Quack. How can I prevent the block from being executed?

 


Ruby implicitly passes the arguments and the block of the current method to super. With arguments, that can be avoided by explicitly calling super with nil as its arguments (super(nil)). The same thing can be done with blocks. While a block can be passed to a method with &,

greet = proc { puts 'hi' } do_some_stuff(&greet) 

you can explicitly avoid passing a block with &nil. In this case, this means you can change the initialize method of TalkingDuck to:

def initialize()   super(&nil)   puts 'I am a duck' end 

and it will explicitly discard any given block and not pass it further up to super, but you can still handle the block yourself inside the method.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: