gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:12:50 25 4
gpt4 key购买 nike

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

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

最佳答案

键入 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'"]

关于ruby - 如何找到调用当前方法的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18714907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com