Project

General

Profile

Bug #6434

Block passed implicitly via super

Added by Thomas Sawyer about 4 years ago. Updated about 4 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
ruby -v:
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
[ruby-core:45036]

Description

=begin
Block is being passed via super implicitly even though the method is explicit and does not pass the block when calling super.

class H < Hash
  def initialize(default=nil, &block)
    super(default)
  end
end
H.new(0){}

ArgumentError: wrong number of arguments
from (pry):3:in `initialize'

Hash#initialize doesn't allow both a default argument and a default_proc, which is the cause of this error. Which means the block is being passed up even though it should not be.
=end

History

#1 [ruby-core:45037] Updated by Thomas Sawyer about 4 years ago

Crap, could an admin please fix the use of RD in my post. And while I am on the subject, can we just make RD mode always on and be done with it?

#2 [ruby-core:45046] Updated by Shyouhei Urabe about 4 years ago

  • Description updated (diff)

#3 [ruby-core:45048] Updated by Nobuyoshi Nakada about 4 years ago

  • Status changed from Open to Rejected

=begin
It's a spec.

(({super})) inherits the given block if it's not given directly.

You can call (({super(default, &nil)})) not to pass it.
=end

#4 [ruby-core:45050] Updated by Thomas Sawyer about 4 years ago

I see. It's not intuitive, obviously, but I suppose it makes sense for how Ruby handles implicit yield in general. Nonetheless, wouldn't it be better if this did not apply when a explicit block argument is used?

# would pass block implicitly
def foo
super
end

# would NOT pass block implicitly
def foo(&block)
super
end

In any case I discovered the use of &nil as a work around prior to posting this and that does the trick. Thanks.

#5 [ruby-core:45051] Updated by Alexey Muranov about 4 years ago

Just a note: super without arguments is documented to pass all arguments, so to not pass a block can only be expected from super()

#6 [ruby-core:45055] Updated by Thomas Sawyer about 4 years ago

@alexey I thought that was no longer true for 1.9.

#7 [ruby-core:45060] Updated by Alexey Muranov about 4 years ago

@Thomas, i didn't know it could change. In fact, i do not know where to find an up to date documentation about basic Ruby keywords.

Also available in: Atom PDF