Class: PryMoves::Recursion::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-moves/recursion_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



7
8
9
10
11
12
13
# File 'lib/pry-moves/recursion_tracker.rb', line 7

def initialize
  @history = []
  @loops = 0
  @missing = 0
  @currently_missing = []
  @missing_lines = []
end

Instance Attribute Details

#loopsObject (readonly)

Returns the value of attribute loops.



5
6
7
# File 'lib/pry-moves/recursion_tracker.rb', line 5

def loops
  @loops
end

Instance Method Details

#apply(result) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/pry-moves/recursion_tracker.rb', line 63

def apply result
  label = "♻️  recursion with #{@loops} loops"
  label += " Ⓜ️  #{@missing} missing lines #{@missing_lines}" if @missing_lines.present?
  label = "...(#{label})..."
  # puts "#{@repetitions_start}..#{@repetitions_end}"
  result[@repetitions_start..@repetitions_end] = [label]
end

#check_recursion(line, bt_index, binding_index) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry-moves/recursion_tracker.rb', line 31

def check_recursion line, bt_index, binding_index
  prev_index = @history.rindex line
  if prev_index == @last_index
    @loops += 1
    @missing = 0
    @recursion_size = 0
    @missing_lines.concat @currently_missing
    @repetitions_end = bt_index
  elsif prev_index && prev_index > @last_index
    @last_index = prev_index + 1
    @recursion_size += 1
    # todo: finish tracking and debug multi-line recursions
  elsif @missing <= @recursion_size
    @missing += 1
    @currently_missing << binding_index
    false
  else
    # @missing_lines = nil
    # @last_index = nil
    @is_finished = true
    false
  end
end

#finished?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/pry-moves/recursion_tracker.rb', line 55

def finished?
  @is_finished
end

#good?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/pry-moves/recursion_tracker.rb', line 59

def good?
  @repetitions_start and @repetitions_end
end

#track(file, line_num, bt_index, binding_index) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pry-moves/recursion_tracker.rb', line 15

def track file, line_num, bt_index, binding_index
  line = "#{file}:#{line_num}"
  if @last_index
    check_recursion line, bt_index, binding_index
  elsif (prev_index = @history.rindex line)
    @loops += 1
    @last_index = prev_index
    @recursion_size = 1
  else
    @history << line
    @last_index = nil
  end

  @repetitions_start ||= bt_index if @loops == 2
end