Module: Byebug::ParseFunctions

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

Constant Summary collapse

Position_regexp =
'(?:(\d+)|(.+?)[:.#]([^.:\s]+))'

Instance Method Summary collapse

Instance Method Details

#get_int(str, cmd, min = nil, max = nil, default = 1) ⇒ 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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/byebug/helper.rb', line 9

def get_int(str, cmd, min = nil, max = nil, default = 1)
  unless str
    return default if default
    print "You need to specify an argument for \"#{cmd}\"\n"
    return nil
  end

  begin
    int = Integer(str)
    if min and int < min
      print "\"#{cmd}\" argument \"#{str}\" needs to be at least #{min}\n"
      return nil
    elsif max and int > max
      print "\"#{cmd}\" argument \"#{str}\" needs to be at most #{max}\n"
      return nil
    end
    return int
  rescue
    print "\"#{cmd}\" argument \"#{str}\" needs to be a number\n"
    return nil
  end
end

#get_line(filename, lineno) ⇒ Object

Gets a single line in a source code file



49
50
51
52
53
# File 'lib/byebug/helper.rb', line 49

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

  return lines[lineno-1]
end

#get_lines(filename) ⇒ Object

Gets all lines in a source code file



35
36
37
38
39
40
41
42
43
44
# File 'lib/byebug/helper.rb', line 35

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

  unless lines = SCRIPT_LINES__[filename]
    lines = File.readlines(filename) rescue []
    SCRIPT_LINES__[filename] = lines
  end

  return lines
end

#syntax_valid?(code) ⇒ Boolean

Returns true if code is syntactically correct for Ruby.

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/byebug/helper.rb', line 58

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