Project

General

Profile

Actions

Bug #21662

closed

Variables other than those in the conditional score are replaced.

Bug #21662: Variables other than those in the conditional score are replaced.

Added by binh (binh chau) about 14 hours ago. Updated about 11 hours ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:123644]

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 Actions #1 [ruby-core:123645]

  • 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

Also available in: PDF Atom