Method: UnboundMethod::Source#get_above_comment_lines

Defined in:
lib/executable/core_ext.rb

#get_above_comment_lines(file, line) ⇒ Object

Get comment from file searching up from given line number.

Parameters:

  • file (String)

    filename, should be full path

  • line (Integer)

    line number in file



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/executable/core_ext.rb', line 49

def get_above_comment_lines(file, line)
  text  = read(file)
  index = line - 1
  while index >= 0 && text[index] !~ /^\s*\#/
    return [] if text[index] =~ /^\s*end/
    index -= 1
  end
  rindex = index
  while text[index] =~ /^\s*\#/
    index -= 1
  end
  result = text[index..rindex]
  result = result.map{ |s| s.strip }
  result = result.reject{ |s| s[0,1] != '#' }
  result = result.map{ |s| s.sub(/^#/,'').strip }
  #result = result.reject{ |s| s == "" }
  result
end