* [ruby-core:68610] [Ruby trunk - Bug #10996] [Open] Inline if statements should hoist variables.
[not found] <redmine.issue-10996.20150323102521@ruby-lang.org>
@ 2015-03-23 10:25 ` josh.cheek
2015-03-23 11:33 ` [ruby-core:68614] [Ruby trunk - Bug #10996] [Rejected] " matz
2015-11-03 22:34 ` [ruby-core:71319] [Ruby trunk - Bug #10996] " josh.cheek
2 siblings, 0 replies; 3+ messages in thread
From: josh.cheek @ 2015-03-23 10:25 UTC (permalink / raw
To: ruby-core
Issue #10996 has been reported by Josh Cheek.
----------------------------------------
Bug #10996: Inline if statements should hoist variables.
https://bugs.ruby-lang.org/issues/10996
* Author: Josh Cheek
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
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.
~~~ruby
if a = 'A'
a # => "A"
end
a # => "A"
~~~
However, when refactoring to an inline-if-statement, the variable is no longer available.
~~~ruby
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.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:68614] [Ruby trunk - Bug #10996] [Rejected] Inline if statements should hoist variables.
[not found] <redmine.issue-10996.20150323102521@ruby-lang.org>
2015-03-23 10:25 ` [ruby-core:68610] [Ruby trunk - Bug #10996] [Open] Inline if statements should hoist variables josh.cheek
@ 2015-03-23 11:33 ` matz
2015-11-03 22:34 ` [ruby-core:71319] [Ruby trunk - Bug #10996] " josh.cheek
2 siblings, 0 replies; 3+ messages in thread
From: matz @ 2015-03-23 11:33 UTC (permalink / raw
To: ruby-core
Issue #10996 has been updated by Yukihiro Matsumoto.
Status changed from Open to Rejected
The fundamental rule is that local variables are defined by the first assignment. For consistency, I will not bend this rule for if modifiers.
Matz.
----------------------------------------
Bug #10996: Inline if statements should hoist variables.
https://bugs.ruby-lang.org/issues/10996#change-51920
* Author: Josh Cheek
* Status: Rejected
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
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.
~~~ruby
if a = 'A'
a # => "A"
end
a # => "A"
~~~
However, when refactoring to an inline-if-statement, the variable is no longer available.
~~~ruby
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.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:71319] [Ruby trunk - Bug #10996] Inline if statements should hoist variables.
[not found] <redmine.issue-10996.20150323102521@ruby-lang.org>
2015-03-23 10:25 ` [ruby-core:68610] [Ruby trunk - Bug #10996] [Open] Inline if statements should hoist variables josh.cheek
2015-03-23 11:33 ` [ruby-core:68614] [Ruby trunk - Bug #10996] [Rejected] " matz
@ 2015-11-03 22:34 ` josh.cheek
2 siblings, 0 replies; 3+ messages in thread
From: josh.cheek @ 2015-11-03 22:34 UTC (permalink / raw
To: ruby-core
Issue #10996 has been updated by Josh Cheek.
Yukihiro Matsumoto wrote:
> The fundamental rule is that local variables are defined by the first assignment. For consistency, I will not bend this rule for if modifiers.
>
> Matz.
That's why it seems like this should be turned on, since it is assigned first. eg the if-statement body would work if it were dynamically accessed.
~~~ruby
eval 'a' if a = 'A' # => "A"
binding.local_variable_get 'b' if b = 'B' # => "B"
~~~
----------------------------------------
Bug #10996: Inline if statements should hoist variables.
https://bugs.ruby-lang.org/issues/10996#change-54694
* Author: Josh Cheek
* Status: Rejected
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
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.
~~~ruby
if a = 'A'
a # => "A"
end
a # => "A"
~~~
However, when refactoring to an inline-if-statement, the variable is no longer available.
~~~ruby
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.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-11-03 22:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <redmine.issue-10996.20150323102521@ruby-lang.org>
2015-03-23 10:25 ` [ruby-core:68610] [Ruby trunk - Bug #10996] [Open] Inline if statements should hoist variables josh.cheek
2015-03-23 11:33 ` [ruby-core:68614] [Ruby trunk - Bug #10996] [Rejected] " matz
2015-11-03 22:34 ` [ruby-core:71319] [Ruby trunk - Bug #10996] " josh.cheek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).