Class: Byebug::TracevarCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/byebug/commands/tracevar.rb

Overview

Show (and possibily stop) at every line that changes a global variable.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

commands, find, format_subcmd, format_subcmds, help, inherited, #initialize, #match

Methods included from StringFunctions

#camelize, #prettify

Methods included from FileFunctions

#get_line, #get_lines, #n_lines, #normalize

Methods included from ParseFunctions

#get_int, #parse_steps, #syntax_valid?

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/byebug/commands/tracevar.rb', line 43

def description
  prettify <<-EOD
    tr[acevar] <variable> [[no]stop]

    Start tracing variable <variable>.

    If "stop" is specified, execution will stop every time the variable
    changes its value. If nothing or "nostop" is specified, execution
    won't stop, changes will just be logged in byebug's output.
  EOD
end

.namesObject



39
40
41
# File 'lib/byebug/commands/tracevar.rb', line 39

def names
  %w(tracevar)
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/byebug/commands/tracevar.rb', line 16

def execute
  var = @match[1]
  return errmsg(pr('trace.errors.needs_global_variable')) unless var

  unless global_variables.include?(:"#{var}")
    return errmsg(pr('trace.errors.var_is_not_global', name: var))
  end

  stop = @match[2] && @match[2] !~ /nostop/

  instance_eval do
    trace_var(:"#{var}") { |val| on_change(var, val, stop) }
  end

  puts pr('trace.messages.success', var: var)
end

#on_change(name, value, stop) ⇒ Object



33
34
35
36
# File 'lib/byebug/commands/tracevar.rb', line 33

def on_change(name, value, stop)
  puts pr('trace.messages.on_change', name: name, value: value)
  byebug(1, false) if stop
end

#regexpObject



10
11
12
13
14
# File 'lib/byebug/commands/tracevar.rb', line 10

def regexp
  /^\s* tr(?:acevar)? (?: \s+ (\S+))?  # (variable-name)?
                      (?: \s+ (stop|nostop))?
   \s*$/x
end