Project

General

Profile

Actions

Bug #11103

closed

Reassignment in if block that is not ran, causes variable to become nil

Added by sigsve (Sigurd Berg Svela) over 9 years ago. Updated over 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
[ruby-core:69004]

Description

I came across a situation where, when reassigning a variable, inside an if statement that is evaluated to false, causes the variable to become nil.
I have included the code to reproduce the problem as a file.

Changing the line

variable = [variable]

to

@variable = [@variable]

seems to work as a temporary workaround, but this still seems to me as a pretty weird bug.


Files

ruby-bug.rb (649 Bytes) ruby-bug.rb Code to reproduce the problem sigsve (Sigurd Berg Svela), 04/27/2015 10:06 PM

Updated by recursive-madman (Recursive Madman) over 9 years ago

Ruby is behaving as it should here. Your confusion is between local variables and method calls.
Before that 'if' block mock_input resolves to the "mock_input" method of your class. The 'if' block then declares a variable with the same name, shadowing the method.

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0