Module: Debugger::ParseFunctions

Defined in:
lib/ruby-debug/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
# File 'lib/ruby-debug/helper.rb', line 9

def get_int(str, cmd, min=nil, max=nil, default=1)
  return default unless str
  begin
    int = Integer(str)
    if min and int < min
      print "%s argument '%s' needs to at least %s.\n" % [cmd, str, min]
      return nil
    elsif max and int > max
      print "%s argument '%s' needs to at most %s.\n" % [cmd, str, max]
      return nil
    end
    return int
  rescue
    print "%s argument '%s' needs to be a number.\n" % [cmd, str]
    return nil
  end
end

#get_onoff(arg, default = nil, print_error = true) ⇒ Object

Return true if arg is ‘on’ or 1 and false arg is ‘off’ or 0. Any other value raises RuntimeError.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby-debug/helper.rb', line 29

def get_onoff(arg, default=nil, print_error=true)
  if arg.nil? or arg == ''
    if default.nil?
      if print_error
        print "Expecting 'on', 1, 'off', or 0. Got nothing.\n"
        raise RuntimeError
      end
      return default
    end
  end
  case arg.downcase
  when '1', 'on'
    return true
  when '0', 'off'
    return false
  else
    if print_error
      print "Expecting 'on', 1, 'off', or 0. Got: %s.\n" % arg.to_s
      raise RuntimeError
    end
  end
end

#show_onoff(bool) ⇒ Object

Return ‘on’ or ‘off’ for supplied parameter. The parmeter should be true, false or nil.



54
55
56
57
58
59
# File 'lib/ruby-debug/helper.rb', line 54

def show_onoff(bool)
  if not [TrueClass, FalseClass, NilClass].member?(bool.class)
    return "??"
  end
  return bool ? 'on' : 'off'
end

#syntax_valid?(code) ⇒ Boolean

Return true if code is syntactically correct for Ruby.

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/ruby-debug/helper.rb', line 62

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