Project

General

Profile

Actions

Bug #19540

closed

Undefined variable error when combining modifier if with one-line pattern matching

Added by mehanoid (Oleg Grigorev) over 2 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
[ruby-core:112942]

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
Actions

Also available in: Atom PDF

Like0
Like0