summaryrefslogtreecommitdiffhomepage
path: root/samples/10_advanced_debugging/01_trace_debugging/app/main.rb
blob: 33f15b3bad41d4a1664e6b4c04e935cde156cc38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Game
  attr_gtk

  def method1 num
    method2 num
  end

  def method2 num
    method3 num
  end

  def method3 num
    method4 num
  end

  def method4 num
    if num == 1
      puts "UNLUCKY #{num}."
      state.unlucky_count += 1
      if state.unlucky_count > 3
        raise "NAT 1 finally occurred. Check app/trace.txt for all method invocation history."
      end
    else
      puts "LUCKY #{num}."
    end
  end

  def tick
    state.roll_history ||= []
    state.roll_history << rand(20) + 1
    state.countdown ||= 600
    state.countdown  -= 1
    state.unlucky_count ||= 0
    outputs.labels << [640, 360, "A dice roll of 1 will cause an exception.", 0, 1]
    if state.countdown > 0
      outputs.labels << [640, 340, "Dice roll countdown: #{state.countdown}", 0, 1]
    else
      state.attempts ||= 0
      state.attempts  += 1
      outputs.labels << [640, 340, "ROLLING! #{state.attempts}", 0, 1]
    end
    return if state.countdown > 0
    method1 state.roll_history[-1]
  end
end

$game = Game.new

def tick args
  trace! $game # <------------------- TRACING ENABLED FOR THIS OBJECT
  $game.args = args
  $game.tick
end