Flowdock
get_file_and_line_from_caller(i=0) public

Returns an array of two elements: the filename where the calling method is located, and the line number where it is defined.

Takes an optional argument i, which specifies how many callers up the stack to look.

Examples:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

def baz
  bar
end

baz
# => ["test.rb", 5]
# => ["test.rb", 9]

If i is not given, or is the default value of 0, it attempts to figure out the correct value. This is useful when in combination with instance_eval. For example:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

instance_eval <<-RUBY, *RSS::Utils.get_file_and_line_from_caller
def baz
  bar
end
RUBY

baz

# => ["test.rb", 8]
Show source
Register or log in to add new notes.