如何找到调用当前方法的方法

时间:2013-09-10 09:13:07

标签: ruby

如何找到调用此方法的位置?

def to_xml(options = {})
  binding.pry
  options = options.to_h if options && options.respond_to?(:to_h)
  serializable_hash(options).to_xml(options)
end

2 个答案:

答案 0 :(得分:8)

输入caller。这将返回当前的调用堆栈。

文档:Kernel#caller

实施例

[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'"]

答案 1 :(得分:1)

    [5] pry(#<MagicOrder::Entities::RefundProduct>)> caller
    => ["/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `eval'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `evaluate_ruby
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:278:in `re'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:254:in `rep'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:234:in `block (3 leve
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `loop'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `block (2 leve
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `catch'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `block in repl
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `catch'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `repl'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_class.rb:170:in `start'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/pry_ext.rb:15:in `bloc
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/processor.rb:18:in `bl
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/processor.rb:17:in `ca
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/processor.rb:17:in `ru
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/pry_ext.rb:14:in `star
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/core_extensions.rb:43:in `pry'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/grape-entity-0.3.0/lib/grape_entity/entity.rb:355:in `to_x
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.14/lib/active_support/xml_mini.rb:109:in
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.14/lib/active_support/core_ext/hash/conv
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.14/lib/active_support/core_ext/hash/conv
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.14/lib/active_support/core_ext/hash/conv
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/builder-3.0.4/lib/builder/xmlbase.rb:170:in `call'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/builder-3.0.4/lib/builder/xmlbase.rb:170:in `_nested_struc
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/builder-3.0.4/lib/builder/xmlbase.rb:63:in `tag!'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/builder-3.0.4/lib/builder/xmlbase.rb:88:in `method_missing
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.14/lib/active_support/core_ext/hash/conv
     "(pry):3:in `__binding__'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `eval'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:328:in `evaluate_ruby
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:278:in `re'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:254:in `rep'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:234:in `block (3 leve
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `loop'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:232:in `block (2 leve
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `catch'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:231:in `block in repl
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `catch'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_instance.rb:230:in `repl'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/pry_class.rb:170:in `start'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-debugger-0.2.2/lib/pry-debugger/pry_ext.rb:19:in `star
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/cli.rb:201:in `block in <top (require
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/cli.rb:70:in `call'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/cli.rb:70:in `block in parse_options'
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/cli.rb:70:in `each'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/lib/pry/cli.rb:70:in `parse_options'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/gems/pry-0.9.12.2/bin/pry:16:in `<top (required)>'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/bin/pry:19:in `load'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/bin/pry:19:in `<main>'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'",
     "/Users/ddl1st/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'"]