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.



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

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

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



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

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.



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

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.



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

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