调用堆栈 "around" ruby 代码

Call stack "around" ruby code

我有一行带有重载方法的 Ruby 代码。我想显示该行的调用堆栈。 我不知道 callerbyebug 的位置。我想显示执行后该行的当前调用堆栈,并找到调试的地方。我该怎么做?

大约像caller Test.method,之后:

    from /var/lib/gems/2.3.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
    from /var/lib/gems/2.3.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /var/lib/gems/2.3.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /var/lib/gems/2.3.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /var/lib/gems/2.3.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'

不完全一样,但很相似。

您可以使用方法method。例如

2.method(:hours)
=> #<Method: Fixnum(Numeric)#hours>

或者可能更详细

2.method(:hours).source_location
=> ["~/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/core_ext/numeric/time.rb", 29]

然后您可以使用调试器(如果可能)或更深入

此答案中的另一个细节How to find where a method is defined at runtime?