Milan Nosáľ Milan Nosáľ - 1 year ago 81
Ruby Question

How to find out from which line number the method was called in Ruby?

Let us have a following simple example:

def funny_function(param)
lineNumber = __LINE__ # this gives me the current line number
puts lineNumber

As we can see, I can get the current line number. However, my question is, is there a non-intrusive way to find out from which line number (and even a file) the method was called?

Non-intrusive meaning that I don't want the method user to know about that, she just has to provide the
parameter, e.g.:

funny_function 'Haha'

Maybe something like

Answer Source

You can use caller_locations which has been added recently. It returns an array of Location objects. See for details.

No need to parse the return of caller. Hooray.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download