module Kernel # Example: # aquire 'foo/bar/*' # requires all files inside foo/bar - recursive # can take multiple parameters def aquire(*files) files.each do |file| require file if %w(rb so).any?{|f| File.file?("#{file}.#{f}")} $:.each do |path| Dir[File.join(path, file, '*.rb')].each do |file| require file end end end end # does a basic require, and prints the message passed as an optional # second parameter if an error occurs. def rescue_require(sym, message = nil) require sym rescue LoadError, RuntimeError puts message if message end # Gives you back the file, line and method of the caller number i # Example: # __caller_info__(1) # -> ['/usr/lib/ruby/1.8/irb/workspace.rb', '52', 'irb_binding'] def __caller_info__(i = 1) file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first end # 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 ] # ] 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 end