Actions
Bug #21662
closedVariables other than those in the conditional score are replaced.
Bug #21662:
Variables 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) about 11 hours 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).
Actions