Project

General

Profile

Actions

Bug #10996

closed

Inline if statements should hoist variables.

Added by josh.cheek (Josh Cheek) about 9 years ago. Updated over 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
[ruby-core:<unknown>]

Description

A multiline if statement hoists local variables to the parent scope. An inline if statement does not.

We can see that in this first example, the a declared in the conditional is available both in the body and in the parent, after the if-statement is executed.

if a = 'A'
  a # => "A"
end
a # => "A"

However, when refactoring to an inline-if-statement, the variable is no longer available.

a if a = 'A'  # ~> NameError: undefined local variable or method `a' for main:Object

# ~> NameError
# ~> undefined local variable or method `a' for main:Object
# ~>
# ~> /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20150323-47886-ay5rau/program.rb:1:in `<main>'

Inline if statements should hoist variables, too. For both consistency and convenience.

Actions

Also available in: Atom PDF

Like0
Like0Like0