Module: Byebug::ParseFunctions

Included in:
CommandProcessor
Defined in:
lib/byebug/helper.rb

Overview

Miscelaneous Utilities

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/byebug/helper.rb', line 10

def get_int(str, cmd, min = nil, max = nil)
  if str !~ /\A[0-9]+\z/
    return nil, "\"#{cmd}\" argument \"#{str}\" needs to be a number"
  end

  int = str.to_i
  if min && int < min
    return nil, "\"#{cmd}\" argument \"#{str}\" needs to be at least #{min}"
  elsif max && int > max
    return nil, "\"#{cmd}\" argument \"#{str}\" needs to be at most #{max}"
  end

  int
end

#get_line(filename, lineno) ⇒ Object

Gets a single line in a source code file



44
45
46
47
48
49
# File 'lib/byebug/helper.rb', line 44

def get_line(filename, lineno)
  lines = get_lines(filename)
  return nil unless lines

  lines[lineno - 1]
end

#get_lines(filename) ⇒ Object

Gets all lines in a source code file



35
36
37
38
39
# File 'lib/byebug/helper.rb', line 35

def get_lines(filename)
  return nil unless File.exist?(filename)

  lines(filename)
end

#lines(filename) ⇒ Object

Fills SCRIPT_LINES__ entry for <filename> if not already filled.



28
29
30
# File 'lib/byebug/helper.rb', line 28

def lines(filename)
  SCRIPT_LINES__[filename] ||= File.readlines(filename)
end

#parse_steps(str, cmd) ⇒ Object

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



64
65
66
67
68
69
70
71
# File 'lib/byebug/helper.rb', line 64

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)


54
55
56
57
58
# File 'lib/byebug/helper.rb', line 54

def syntax_valid?(code)
  eval("BEGIN {return true}\n#{code}", nil, '', 0)
rescue SyntaxError
  false
end