Objects and Classes
Instance Variables
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
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}"
step ← 2
15step→ 2 = 2 #@step=1, 516counter = Counter.new@count ← 0
1class Counter2 def initialize3 @count→ 0 = 04 endcounter ← ⟨Counter A @count=0⟩
15step = 2 #@step=1, 516counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)219counter.add(step)def add(amount)
pass 1 of 26def add(amount2)7 @count2 += amount28endcounter.add(step)
18counter.add(step)219counter.add(step)8def add(amount)
pass 2 of 26def add(amount2)7 @count8 += amount28endcounter.add(step)
18counter.add(step)19counter.add(step)82021puts "step=#{step2}"22puts "count=#{counter.count12}"outputstep=2def count
10def count11 @count1212endputs "count=#{counter.count}"
21puts "step=#{step}"22puts "count=#{counter.count12}"outputcount=12
step ← 1
15step→ 1 = 116counter = Counter.new@count ← 0
1class Counter2 def initialize3 @count→ 0 = 04 endcounter ← ⟨Counter A @count=0⟩
15step = 116counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)119counter.add(step)def add(amount)
pass 1 of 26def add(amount1)7 @count1 += amount18endcounter.add(step)
18counter.add(step)119counter.add(step)4def add(amount)
pass 2 of 26def add(amount1)7 @count4 += amount18endcounter.add(step)
18counter.add(step)19counter.add(step)42021puts "step=#{step1}"22puts "count=#{counter.count6}"outputstep=1def count
10def count11 @count612endputs "count=#{counter.count}"
21puts "step=#{step}"22puts "count=#{counter.count6}"outputcount=6
step ← 5
15step→ 5 = 516counter = Counter.new@count ← 0
1class Counter2 def initialize3 @count→ 0 = 04 endcounter ← ⟨Counter A @count=0⟩
15step = 516counter→ ⟨Counter A @count=0⟩ = Counter.new1718counter.add(step)519counter.add(step)def add(amount)
pass 1 of 26def add(amount5)7 @count5 += amount58endcounter.add(step)
18counter.add(step)519counter.add(step)20def add(amount)
pass 2 of 26def add(amount5)7 @count20 += amount58endcounter.add(step)
18counter.add(step)19counter.add(step)202021puts "step=#{step5}"22puts "count=#{counter.count30}"outputstep=5def count
10def count11 @count3012endputs "count=#{counter.count}"
21puts "step=#{step}"22puts "count=#{counter.count30}"outputcount=30
Follow the Count
stepstarts as2.Counter.newstarts with@count = 0.- The first
counter.add(step)adds2. - The second
counter.add(step)adds another2. counter.countreturns4. | 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.