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, #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



40
41
42
43
44
45
46
47
48
49
# File 'lib/byebug/commands/condition.rb', line 40

def description
  prettify <<-EOD
    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.
  EOD
end

.namesObject



36
37
38
# File 'lib/byebug/commands/condition.rb', line 36

def names
  %w(condition)
end

Instance Method Details

#executeObject



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

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

  breakpoints = Byebug.breakpoints.sort_by(&:id)
  return errmsg(pr('condition.errors.no_breakpoints')) if breakpoints.empty?

  pos, err = get_int(@match[1], 'Condition', 1)
  return errmsg(err) if err

  breakpoint = breakpoints.find { |b| b.id == pos }
  return errmsg(pr('break.errors.no_breakpoint')) unless breakpoint

  unless syntax_valid?(@match[2])
    return errmsg(pr('break.errors.not_changed', expr: @match[2]))
  end

  breakpoint.expr = @match[2]
end

#regexpObject



12
13
14
# File 'lib/byebug/commands/condition.rb', line 12

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