
Issue #19540 has been updated by jeremyevans0 (Jeremy Evans). 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. ---------------------------------------- Bug #19540: Undefined variable error when combining modifier if with one-line pattern matching https://bugs.ruby-lang.org/issues/19540#change-102467 * Author: mehanoid (Oleg Grigorev) * Status: Rejected * Priority: Normal * ruby -v: ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- 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 ``` -- https://bugs.ruby-lang.org/