Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the method which called the current method

Tags:

ruby

How do I find where this method is called?

def to_xml(options = {})
  binding.pry
  options = options.to_h if options && options.respond_to?(:to_h)
  serializable_hash(options).to_xml(options)
end
like image 903
plusor Avatar asked Sep 10 '13 09:09

plusor


1 Answers

Type caller. That will return current call stack.

Documentation: Kernel#caller.

Example

[0] % rspec spec
 10/16 |=================================================== 62 ====================================================>                                                                  |  ETA: 00:00:00
From: /Volumes/MacintoshHD/Users/sergio/projects/gt2/spec/models/app_spec.rb @ line 14 :

     9:   it { should belong_to :user }
    10:
    11:
    12:   describe '#app_key' do
    13:     it 'is generated for new user' do
 => 14:       binding.pry
    15:       app = FactoryGirl.build(:app)
    16:       app.app_key.should be_nil
    17:
    18:       app.save!
    19:       app.app_key.should_not be_nil

[1] pry(#<RSpec::Core::ExampleGroup::Nested_2::Nested_1>)> caller
=> ["/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `eval'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `evaluate_ruby'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:278:in `re'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:254:in `rep'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:234:in `block (3 levels) in repl'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `loop'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `block (2 levels) in repl'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `catch'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `block in repl'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `catch'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `repl'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/pry_class.rb:170:in `start'",
 "/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/pry-0.9.12.2/lib/pry/core_extensions.rb:43:in `pry'",
 "/Volumes/MacintoshHD/Users/sergio/projects/gt2/spec/models/app_spec.rb:14:in `block (3 levels) in <top (required)>'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:254:in `with_around_each_hooks'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'",
 "/Volumes/MacintoshHD/Users/sergio/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'"]
like image 180
Sergio Tulentsev Avatar answered Oct 02 '22 13:10

Sergio Tulentsev