Project

General

Profile

Actions

Feature #11325

closed

Block is passed to initializer implicitly even when I asked not to.

Added by nepalez (Andrew Kozin) almost 9 years ago. Updated almost 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:69843]

Description

This works as expected:

class Foo
  attr_reader :block

  def initialize(&block)
    @block = block
  end
end

foo = Foo.new { :foo }
foo.block.nil?
# => false

But then I'm trying to stop passing a block to the superclass' initializer and cannot do this:

class Bar < Foo
  def initialize(&block)
    block = nil # added it, however this shouldn't be necessary
    yield       # nor this one
    super()     # explicitly asked not to pass the block
  end
end

bar = Bar.new { :foo }
bar.block.nil?
# => false (expected true)

Adding an empty block is not the solution, because what is really needed is stop passing a block.

Updated by marcandre (Marc-Andre Lafortune) almost 9 years ago

  • Status changed from Open to Rejected

It may be surprising to you, but this is by design.

You have to explicitly pass an empty block when using super:

super(&nil)

There's also no way to change this even if we wanted to because of incompatibilities.

Actions

Also available in: Atom PDF

Like0
Like0