Sha256: 63e01a7e5873157101702b4d201aa8e7ea1709e9b264d1358bca01741ca6b86e

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

require 'byebug/command'

module Byebug
  #
  # Show (and possibily stop) at every line that changes a global variable.
  #
  class TracevarCommand < Command
    self.allow_in_post_mortem = false

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

    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

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

    class << self
      def names
        %w(tracevar)
      end

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
byebug-4.0.3 lib/byebug/commands/tracevar.rb
byebug-4.0.2 lib/byebug/commands/tracevar.rb
byebug-4.0.1 lib/byebug/commands/tracevar.rb
byebug-4.0.0 lib/byebug/commands/tracevar.rb