Project

General

Profile

Bug #16344

Can be assigned to numbered parameter when after used _1

Added by osyo (manga osyo) 8 months ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-11-11T23:39:38Z trunk 2407e89725) [x86_64-linux]
[ruby-core:95806]
Tags:

Description

Steps to reproduce

  1. Define local variable _1 outside block
  2. Call Numbered parameter in block
  3. Assign for _1

Expected behavior

_1 = :local_variable
proc {
  _1
  # Error: Can't assign to numbered parameter _1
  _1 = 42
}

Actual behavior

_1 = :local_variable
proc {
  _1
  # No error
  _1 = 42
}

Cannot be assinged to numbered parameter from #16293.
However, can be assinged to numbered parameter if defined local variable _1 outside block.
I think this is a bug.

#1

Updated by osyo (manga osyo) 8 months ago

  • Description updated (diff)

Updated by Hanmac (Hans Mackowiak) 8 months ago

i think this has something to do with that _* parameter doesn't cause duplicate syntax errors:

def abc(_x,_x)
  p _x
end

abc("y","z") #=> "y"

See some of your examples from #16293

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

  • Status changed from Open to Closed

Assigning to the numbered parameter is now a SyntaxError even if it is a local variable in scope, so current master has your expected behavior.

Related to this, accessing the numbered parameter inside a block in master accesses the numbered parameter, even if a local variable of the same name exists in the outer scope:

_1 = :local_variable
proc{_1}.call(:block_arg)
# 2.7: => :local_variable
# 3.0: => :block_arg

I think we should make assigning to a numbered parameter a SyntaxError instead of a warning outside of a block (it was a warning in 2.7). I'll submit a feature request for that.

Also available in: Atom PDF