Class: Byebug::ConditionCommand

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

Overview

Implements conditions on breakpoints.

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

Instance Attribute Summary

Attributes inherited from Command

#processor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ParseHelper

#get_int, #parse_steps, #syntax_valid?

Methods inherited from Command

#arguments, columnize, #context, #frame, help, #initialize, match, to_s

Methods included from Helpers::StringHelper

#camelize, #deindent, #prettify

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



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

def self.description
  <<-DESCRIPTION
    cond[ition] <n>[ expr]

    #{short_description}

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

.regexpObject



17
18
19
# File 'lib/byebug/commands/condition.rb', line 17

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

.short_descriptionObject



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

def self.short_description
  "Sets conditions on breakpoints"
end

Instance Method Details

#executeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/byebug/commands/condition.rb', line 38

def execute
  return puts(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

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

  breakpoint.expr = @match[2]
end