Module: Byebug::ParseFunctions

Included in:
Command, Runner
Defined in:
lib/byebug/helper.rb

Overview

Utilities to assist command parsing

Instance Method Summary collapse

Instance Method Details

#get_int(str, cmd, min = nil, max = nil) ⇒ Object

Parse ‘str’ of command ‘cmd’ as an integer between min and max. If either min or max is nil, that value has no bound.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/byebug/helper.rb', line 74

def get_int(str, cmd, min = nil, max = nil)
  if str !~ /\A[0-9]+\z/
    return nil, pr('parse.errors.int.not_number', cmd: cmd, str: str)
  end

  int = str.to_i
  if min && int < min
    return min, pr('parse.errors.int.too_low', cmd: cmd, str: str, min: min)
  elsif max && int > max
    return max, pr('parse.errors.int.too_high',
                   cmd: cmd, str: str, max: max)
  end

  int
end

#parse_steps(str, cmd) ⇒ Object

Returns the number of steps specified in <str> as an integer or 1 if <str> is empty.



122
123
124
125
126
127
128
129
# File 'lib/byebug/helper.rb', line 122

def parse_steps(str, cmd)
  return 1 unless str

  steps, err = get_int(str, cmd, 1)
  return nil, err unless steps

  steps
end

#syntax_valid?(code) ⇒ Boolean

Returns true if code is syntactically correct for Ruby

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/byebug/helper.rb', line 93

def syntax_valid?(code)
  return true unless code

  without_stderr do
    begin
      RubyVM::InstructionSequence.compile(code)
      true
    rescue SyntaxError
      false
    end
  end
end

#without_stderrObject

Temporarily disable output to $stderr



109
110
111
112
113
114
115
116
# File 'lib/byebug/helper.rb', line 109

def without_stderr
  stderr = $stderr
  $stderr.reopen(IO::NULL)

  yield
ensure
  $stderr.reopen(stderr)
end