Module: Trepan::Util

Defined in:
app/util.rb

Class Method Summary collapse

Class Method Details

.extract_expression(text) ⇒ Object

extract the “expression” part of a line of source code.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/util.rb', line 28

def extract_expression(text)
  if text =~ /^\s*(?:if|elsif|unless)\s+/
    text.gsub!(/^\s*(?:if|elsif|unless)\s+/,'')
    text.gsub!(/\s+then\s*$/, '')
  elsif text =~ /^\s*(?:until|while)\s+/
    text.gsub!(/^\s*(?:until|while)\s+/,'')
    text.gsub!(/\s+do\s*$/, '')
  elsif text =~ /^\s*return\s+/
    # EXPRESION in: return EXPRESSION
    text.gsub!(/^\s*return\s+/,'')
  elsif text =~ /^\s*case\s+/
    # EXPRESSION in: case EXPESSION
    text.gsub!(/^\s*case\s*/,'')
  elsif text =~ /^\s*def\s*.*\(.+\)/
    text.gsub!(/^\s*def\s*.*\((.*)\)/,'[\1]')
  elsif text =~ /^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]/
    # RHS of an assignment statement.
    text.gsub!(/^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=/,'')
  end
  return text
end

.find_main_script(locs) ⇒ Object

Find user portion of script skipping over Rubinius code loading. Unless hidestack is off, we don’t show parts of the frame below this.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/util.rb', line 59

def find_main_script(locs)
  candidate = nil
  (locs.size-1).downto(0) do |i|
    loc = locs[i]
    if rubinius_internal?(loc)
      if loc.method.active_path =~ /\/trepanx$/ ||
        loc.method.active_path == 'kernel/loader.rb'
        # Might have been run from standalone trepanx.
        candidate = i
      else
        return locs.size - i
      end
    end
  end
  candidate ? locs.size - candidate - 1 : nil
end

.rubinius_internal?(loc) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'app/util.rb', line 51

def rubinius_internal?(loc)
  ('Object#' == loc.describe_receiver &&
   :__script__ == loc.name && RedCard.check('1.8')) or
    ('Rubinius::Loader#' == loc.describe_receiver && RedCard.check('1.9'))
end

.safe_repr(str, max, elipsis = '... ') ⇒ Object



8
9
10
11
12
13
14
# File 'app/util.rb', line 8

def safe_repr(str, max, elipsis='... ')
  if str.is_a?(String) && max > 0 && str.size > max && !str.index("\n")
    "%s%s%s" % [ str[0...max/2], elipsis,  str[str.size-max/2..str.size]]
  else
    str
  end
end

.suppress_warningsObject

Suppress warnings. The main one we encounter is “already initialized constant” because perhaps another version readline has done that already.



78
79
80
81
82
83
84
# File 'app/util.rb', line 78

def suppress_warnings
  original_verbosity = $VERBOSE
  $VERBOSE = nil
  result = yield
  $VERBOSE = original_verbosity
  return result
end

.uniq_abbrev(list, name) ⇒ Object

name is String and list is an Array of String. If name is a unique leading prefix of one of the entries of list, then return that. Otherwise return name.



19
20
21
22
23
24
# File 'app/util.rb', line 19

def uniq_abbrev(list, name)
  candidates = list.select do |try_name|
    try_name.start_with?(name)
  end
  candidates.size == 1 ? candidates.first : name
end