Instance variables hold object state that methods can read and update.

instance variable An instance variable starts with `@` and belongs to one object instance.

Instance Variables

step
instance_variables.rb
Replay: real traced execution (multi-file project)
class Counter
  def initialize
    @count = 0
  end

  def add(amount)
    @count += amount
  end

  def count
    @count
  end
end

step = 2
counter = Counter.new

counter.add(step)
counter.add(step)

puts "step=#{step}"
puts "count=#{counter.count}"
class Counter
  def initialize
    @count = 0
  end

  def add(amount)
    @count += amount
  end

  def count
    @count
  end
end

step = 1
counter = Counter.new

counter.add(step)
counter.add(step)

puts "step=#{step}"
puts "count=#{counter.count}"
class Counter
  def initialize
    @count = 0
  end

  def add(amount)
    @count += amount
  end

  def count
    @count
  end
end

step = 5
counter = Counter.new

counter.add(step)
counter.add(step)

puts "step=#{step}"
puts "count=#{counter.count}"
  1. step ← 2

    15step→ 2 = 2  #@step=1, 516counter = Counter.new
  2. @count ← 0

    1class Counter2  def initialize3    @count→ 0 = 04  end
  3. counter ← ⟨Counter A @count=0⟩

    15step = 2  #@step=1, 516counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)219counter.add(step)
  4. def add(amount)

    pass 1 of 2
    6def add(amount2)7  @count2 += amount28end
  5. counter.add(step)

    18counter.add(step)219counter.add(step)8
  6. def add(amount)

    pass 2 of 2
    6def add(amount2)7  @count8 += amount28end
  7. counter.add(step)

    18counter.add(step)19counter.add(step)82021puts "step=#{step2}"22puts "count=#{counter.count12}"
    outputstep=2
  8. def count

    10def count11  @count1212end
  9. puts "count=#{counter.count}"

    21puts "step=#{step}"22puts "count=#{counter.count12}"
    outputcount=12
  1. step ← 1

    15step→ 1 = 116counter = Counter.new
  2. @count ← 0

    1class Counter2  def initialize3    @count→ 0 = 04  end
  3. counter ← ⟨Counter A @count=0⟩

    15step = 116counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)119counter.add(step)
  4. def add(amount)

    pass 1 of 2
    6def add(amount1)7  @count1 += amount18end
  5. counter.add(step)

    18counter.add(step)119counter.add(step)4
  6. def add(amount)

    pass 2 of 2
    6def add(amount1)7  @count4 += amount18end
  7. counter.add(step)

    18counter.add(step)19counter.add(step)42021puts "step=#{step1}"22puts "count=#{counter.count6}"
    outputstep=1
  8. def count

    10def count11  @count612end
  9. puts "count=#{counter.count}"

    21puts "step=#{step}"22puts "count=#{counter.count6}"
    outputcount=6
  1. step ← 5

    15step→ 5 = 516counter = Counter.new
  2. @count ← 0

    1class Counter2  def initialize3    @count→ 0 = 04  end
  3. counter ← ⟨Counter A @count=0⟩

    15step = 516counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)519counter.add(step)
  4. def add(amount)

    pass 1 of 2
    6def add(amount5)7  @count5 += amount58end
  5. counter.add(step)

    18counter.add(step)519counter.add(step)20
  6. def add(amount)

    pass 2 of 2
    6def add(amount5)7  @count20 += amount58end
  7. counter.add(step)

    18counter.add(step)19counter.add(step)202021puts "step=#{step5}"22puts "count=#{counter.count30}"
    outputstep=5
  8. def count

    10def count11  @count3012end
  9. puts "count=#{counter.count}"

    21puts "step=#{step}"22puts "count=#{counter.count30}"
    outputcount=30

Follow the Count

  1. step starts as 2.
  2. Counter.new starts with @count = 0.
  3. The first counter.add(step) adds 2.
  4. The second counter.add(step) adds another 2.
  5. counter.count returns 4. | action | count after | | --- | --- | | new counter | 0 | | first add 2 | 2 | | second add 2 | 4 |

Exercise: instance_variables.rb

Reproduce step=2 and count=4, then use the pinned step variants 1 and 5 to predict count=2 and count=10.