0

Basically the same question asked here, but then a Ruby solution.

I have a class, with static functions that fill the class:

class Message
  attributes :message, :origin

  def initialize(params)
   self.origin = initialize_called_by
   super
  end

  def self.failed_to_save
    Message.new(message: 'Failed to save that thing!')
  end

  def self.failed_to_delete
    Message.new(message: 'Failed to delete that thing!')
  end
end

Where I'd expect origin to contain the following:

Message.failed_to_save.origin # failed_to_save
Message.failed_to_delete.origin # failed_to_delete
Gijs P
  • 1,325
  • 12
  • 28

1 Answers1

1
caller_locations(1,1)[0].label

If you extract this into a method, you might need to modify the first argument of caller_locations to look further up the stack trace.

Siim Liiser
  • 3,860
  • 11
  • 13