Actions
Bug #19540
closedUndefined variable error when combining modifier if with one-line pattern matching
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
Description
If I try to use modifier if
with a one-line pattern matching, the variable assigned by the pattern cannot be used inside the expression to the left of the "if":
x if [1] in [x]
(irb):1:in `<main>': undefined local variable or method `x' for main:Object (NameError)
This is very unexpected behavior, because the code looks equivalent to the following:
if [1] in [x]
x
end
=> 1
Updated by jeremyevans0 (Jeremy Evans) over 2 years ago
- Status changed from Open to Rejected
This isn't a bug, this is how Ruby works. It is unrelated to pattern matching, you get the same behavior for x if x = foo
and x unless x = foo
. It may be unexpected if you are not used to it, though.
Actions
Like0
Like0