I was about to run a rake task which would make important changes in my database.
My code was similar to this:
rubydef migrate_active_accounts
accounts = Account.active
end
def migrate_inactive_accounts
accounts = Account.inactive
end
This migration was very critical. Thus, I decided to log everything to make sure it would run as expected.
So I wrote a separate method to log what accounts were going to run by which method.
In order to do that, I'd need to know which method was calling my logger method.
Solution
As usual, Ruby has a neat solution for it:
Kernel
module has a
caller_locations
method, which returns an array with the current execution stack. So all we need to do is to use
caller_locations.first.label
to get the first element from the stack and then get its label.
My final code was looking like the following:
rubydef migrate_active_accounts
accounts = Account.active
log_migration_info(accounts)
end
def migrate_inactive_accounts
accounts = Account.inactive
log_migration_info(accounts)
end
private
def log_migration_info(accounts)
caller_method = caller_locations.first.label
Rails.logger.info "Running [MyClass##{caller_method}]
for the following accounts: #{accounts.map(&:id).to_sentence}"
end
Ruby prior to 2.0
If you're using an old version of Ruby (
even though you shouldn't),
you'll need to use
caller
method along with a regular expression:
rubycaller.first[/`(.*)'/, 1]
Happy hacking!
Post updated at 02/10/2017, 10:00:00
💬 Comments are temporarily disabled. Sorry for the inconvenience.