I have a method foo
and it is called in a script script001.rb
how should I write the foo
method so that it returns the file name of a script that called it?
To avoid needing to deal with caller
style strings, you can use Kernel#caller_locations
, instead. It returns you an array of Thread::Backtrace::Location
objects, which has some convenient methods available for you.
To get the filename, in your case, you can use the #path
method:
def foo
caller_locations.first.path
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With