Class: Byebug::ConditionCommand

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

Overview

Implements conditions on breakpoints.

Adds the ability to stop on breakpoints only under certain conditions.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



36
37
38
39
40
41
42
43
# File 'lib/byebug/commands/condition.rb', line 36

def description
  %(cond[ition] <n>[ expr]

    Specify breakpoint number <n> to break only if <expr> is true. <n> is
    an integer and <expr> is an expression to be evaluated whenever
    breakpoint <n> is reached. If no expression is specified, the
    condition is removed.)
end

.namesObject



32
33
34
# File 'lib/byebug/commands/condition.rb', line 32

def names
  %w(condition)
end

Instance Method Details

#executeObject



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

def execute
  return puts(ConditionCommand.help) unless @match[1]

  breakpoints = Byebug.breakpoints.sort_by { |b| b.id }
  return errmsg('No breakpoints have been set') unless breakpoints.any?

  pos, err = get_int(@match[1], 'Condition', 1, breakpoints.last.id)
  return errmsg(err) unless pos

  breakpoint = breakpoints.select { |b| b.id == pos }.first

  return errmsg("Incorrect expression \"#{@match[2]}\", " \
                'breakpoint not changed') unless syntax_valid?(@match[2])

  breakpoint.expr = @match[2]
end

#regexpObject



10
11
12
# File 'lib/byebug/commands/condition.rb', line 10

def regexp
  /^\s* cond(?:ition)? (?:\s+(\d+)(?:\s+(.*))?)? \s*$/x
end