Project

General

Profile

Actions

Bug #17619

closed

if false foo=42; end creates a foo local variable set to nil

Added by pkmuldoon (Phil Muldoon) about 3 years ago. Updated about 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:102438]

Description

Take this following code

[1] pry(main)> defined?(foo)
nil

[2] pry(main)> if false
[2] pry(main)*   foo = 42
[2] pry(main)* end  

[3] pry(main)> defined?(foo)
"local-variable"

The inner scope inherits the parent scope (ok) but also modifies the parent scope even if the child scope is never entered. A lesser effect of this:

[1] pry(main)> defined?(bar)
nil

[2] pry(main)> if false
[2] pry(main)*   bar = 99
[2] pry(main)* end  

[3] pry(main)> defined?(bar)
"local-variable"

[5] pry(main)> bar
99

That somewhat lesser affecting because I can just about accept a variable invading the parent scope, and existing after, as a hoisting event. But surely that should not be the case in the negative program-flow case?

The side effects of this are defined?(foo) can't be trusted anymore.

Apologies if this bug has been filed. I did search for it, but couldn't find anything quite matching it. Thanks!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0