From: "mehanoid (Oleg Grigorev) via ruby-core" Date: 2023-03-18T14:33:49+00:00 Subject: [ruby-core:112942] [Ruby master Bug#19540] Undefined variable error when combining modifier if with one-line pattern matching Issue #19540 has been reported by mehanoid (Oleg Grigorev). ---------------------------------------- Bug #19540: Undefined variable error when combining modifier if with one-line pattern matching https://bugs.ruby-lang.org/issues/19540 * Author: mehanoid (Oleg Grigorev) * Status: Open * 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 `
': 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/