Project

General

Profile

Feature #12214

Inconsistent behaviour and lack of warnings/errors when referencing duplicated _variables

Added by Carpetsmoker (Martin Tournoij) 12 months ago. Updated 12 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:74538]

Description

_, _ = [1, 2]
p _

gives 2

def foo _, _
    p _
end
foo 1, 2

gives 1

def foo _, _=2
    p _
end
foo 1

gives 2

In other words, referencing duplicated variables starting with an underscore gives inconsistent results.

In the first example it makes since since it's basically:

_ = 1
_ = 2

In the method it makes a bit less sense, especially since the behaviour is inconsistent.

My suggested fix would be to at least make referencing duplicated _variables a warning with ruby -w. Perhaps it also makes sense to do this for all _variables, since the underscore indicates you should not reference them...

It could, perhaps, even me made an error to reference duplicated variables like this. The behaviour is surprising, and likely to change in future versions of Ruby...

History

#1 [ruby-core:74539] Updated by Carpetsmoker (Martin Tournoij) 12 months ago

Oh, I forgot to add the third case:

def foo _, _=2
    p _
end
foo 1, 3

gives 1 (and not 3).

Also available in: Atom PDF