public Method

Kernel.__caller_lines__(file, line, size = 4)

Gives you some context around a specific line in a file. the size argument works in both directions + the actual line, size = 2 gives you 5 lines of source, the returned array has the following format.

[
  line = [
           lineno           = Integer,
           line             = String,
           is_searched_line = (lineno == initial_lineno)
         ],
  ...,
  ...
]

Example

__caller_lines__('/usr/lib/ruby/1.8/debug.rb', 122, 2) # ->
 [
   [ 120, "  def check_suspend",                               false ],
   [ 121, "    return if Thread.critical",                     false ],
   [ 122, "    while (Thread.critical = true; @suspend_next)", true  ],
   [ 123, "      DEBUGGER__.waiting.push Thread.current",      false ],
   [ 124, "      @suspend_next = false",                       false ]
 ]

Source Code

# File merb/core_ext/kernel.rb, line 170
def __caller_lines__(file, line, size = 4)
  return [['Template Error!', "problem while rendering", false]] if file =~ /\(erubis\)/
  lines = File.readlines(file)
  current = line.to_i - 1

  first = current - size
  first = first < 0 ? 0 : first

  last = current + size
  last = last > lines.size ? lines.size : last

  log = lines[first..last]

  area = []

  log.each_with_index do |line, index|
    index = index + first + 1
    area << [index, line.chomp, index == current + 1]
  end

  area
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.