Project

General

Profile

Actions

Bug #20505

closed

Reassigning the block argument in method body keeps old block when calling super with implicit arguments

Added by Earlopain (Earlopain _) 6 months ago. Updated 4 months ago.

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

Description

You can call super without arguments and parenthesis to pass along all the enclosing method arguments to the parent method.

You can modify positional and keyword arguments before the call to super, and the parent method will recieve these modified values. With the block arg however that isn't the case:

class A
  def positional_arg(a)
    puts a
  end

  def block_arg(&block)
    yield
  end
end

class B < A
  def positional_arg(a = nil)
    a = 'b'
    super
  end

  def block_arg(&block)
    block = proc { puts 'b' }
    super
  end
end

B.new.positional_arg('a')
B.new.positional_arg
B.new.block_arg { puts 'a' }
B.new.block_arg

I would expect this snippet to print b four times. The actual output is b b a and LocalJumpError. To get the desired output I must pass the block along explicitly with super(&block).

I hope my example explains the issue good enough. I have looked through issues here and searched for documentation and haven't found any mention of this behaviour. Sorry if I missed something somewhere.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0