Search code examples
rubyexceptionraise

Passing arguments to `caller` statement in `raise` statement


I cannot follow what caller does in these exception raising statements.

raise InterfaceException , "Error", caller

raise ArgumentError, " Error", caller[1..-1]

I know that Object#caller sets and sends stack trace to upper level in hierarchy. What is the interpretation of the arguments 1..-1 of the method caller?


Solution

  • As you noted, caller returns current stack trace (not including current method). caller[1..-1] returns the stacktrace, minus its first entry.

    Might be useful in some situations. For example you set up a params validation handler (or whatever) and it can raise. But you don't want to see the error originate in the validation handler. You want the line that called it.