Bug #21662
closedVariables other than those in the conditional score are replaced.
Description
class Bar
attr_accessor :name
end
class Foo
attr_accessor :bar
def initialize(bar=nil)
@bar = bar || Bar.new
end
def bug!
if !bar.is_a?(Bar)
bar = ''
elsif bar.name == 'bar'
end
end
end
Foo.new.bug!
Updated by nobu (Nobuyoshi Nakada) 1 day ago
- Description updated (diff)
- Status changed from Open to Feedback
I try to guesstimate your intension by telepathy.
binh (binh chau) wrote:
def bug! if !bar.is_a?(Bar) bar = '' elsif bar.name == 'bar' end end
You might had expected the third bar is a Bar.
In fact, the first bar in !bar.is_a?(Bar) is the attribute.
But the assignment in the next line makes the local variable bar regardless whether this line is executed.
So all bar after here in this method, including in the next elsif line, are this local variable, that is an empty string or nil (when the elsif condition is executed).
Updated by binh (binh chau) about 12 hours ago
nobu (Nobuyoshi Nakada) wrote in #note-1:
I try to guesstimate your intension by telepathy.
binh (binh chau) wrote:
def bug! if !bar.is_a?(Bar) bar = '' elsif bar.name == 'bar' end endYou might had expected the third
baris aBar.In fact, the first
barin!bar.is_a?(Bar)is the attribute.
But the assignment in the next line makes the local variablebarregardless whether this line is executed.
So allbarafter here in this method, including in the nextelsifline, are this local variable, that is an empty string ornil(when theelsifcondition is executed).
It looks like a trap.