【问题标题】:How do I find where a Ruby method is called at runtime?如何找到运行时调用 Ruby 方法的位置?
【发布时间】:2011-05-05 03:15:36
【问题描述】:

此特定方法导致应用程序崩溃:

# needed becasuse of a rails bug
def to_s
  "#{self.class.name.underscore}__#{object_id}"
end  

出现以下错误:

ActionView::TemplateError (`@content_for_details_view_builder__2234321380' is not allowed as an instance variable name) in app/views/shared/_details_view.haml:

基本上这是导致上述异常的代码执行:

# controller
def new
  @view_mode = :new
  template = "new"
  render template, :layout => false
end    

# new.haml
- details_view @user do |b, f|

# build_helper
def details_view(model, options = {}, &block)
  # instantiate OptionsWrapper passing a hash of key/value pairs, in this case
  # an empty hash. OptionsWrapper contains a method_missing method so when we
  # invoke mode on our instance of OptionsWrapper, since it does not contain
  # instance method of mode, it invokes method_missing and we set a number of
  # methods on fly.  
  options = OptionsWrapper.new(options) 
  options.mode ||= @view_mode
  model_name = model.class.model_name.singular.dasherize
  options.dom_id = "#{model_name}-details-view"
  # we instantiate DetailsViewBuilder passing in a few arguments (e.g. new,
  # ActionView, user) to the constructor method.
  builder = DetailsViewBuilder.new(options.mode, self, model)
  options.html ||= {}
  options.html[:id] ||= nil
  options.html[:class] = "x-#{options.mode} #{options.html[:class]}".strip
  render :layout => "shared/details_view", :locals => {:builder => builder,
    :model => model, :options => options}, &block
end

#details_view_builder
class DetailsViewBuilder
  attr_accessor :mode, :template, :model, :form

  def initialize(mode, template, model)
    @mode, @template, @model = mode, template, model #new, ActionView, user
  end

  def to_s
    # here's the method causing error!
    "#{self.class.name.underscore}__#{object_id}"
  end
end

# we then render the details_view passing in some locals as shown a
# few lines above:
.details-dialog{:id => options.dom_id }
  case options.mode
  - when :new
- form_for model, options.slice(:url, :html) do |form|
  = yield builder, form

我 100% 确定是 to_s 方法导致了错误。我不确定在哪里调用它会导致此错误。注意上面它产生了builder变量。该构建器变量包含具有此方法的类。我需要找到一种更好的方法来检查元素,这样我就可以透明地看到调用堆栈,并且给定一个方法,我知道它在哪里被调用。奇怪的是,这种方法适用于我的本地机器,但会导致服务器上的生产出错。这是回溯的花絮:

ActionView::TemplateError
(`@content_for_details_view_builder__-626960428' is not allowed as an
instance variable name) in app/views/shared/_details_view.haml:

searchlogic (2.4.27) lib/searchlogic/rails_helpers.rb:75:in
`fields_for'
searchlogic (2.4.27) lib/searchlogic/rails_helpers.rb:64:in
`form_for'
app/helpers/builders_helper.rb:68:in `details_view'
/home/app-sptr/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/benchmark.rb:308:in
`realtime'
app/other/restful_component.rb:488:in `new_html'
app/other/restful_component.rb:73:in `new'
app/other/restful_component.rb:72:in `new'
/home/app-sptr/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/benchmark.rb:308:in
`realtime'
rack (1.1.2) lib/rack/head.rb:9:in `call'
rack (1.1.2) lib/rack/methodoverride.rb:24:in `call'
rack (1.1.2) lib/rack/lock.rb:11:in `call'
rack (1.1.2) lib/rack/lock.rb:11:in `synchronize'
rack (1.1.2) lib/rack/lock.rb:11:in `call'

我也在这里问过问题,但对答案不满意: http://www.ruby-forum.com/topic/1641800#new

【问题讨论】:

    标签: ruby methods callstack inspect


    【解决方案1】:

    使用caller() 的建议非常适合从某种方法中找出堆栈跟踪。不过,我也会分享(我认为是)解决您问题的方法。

    to_s 方法返回一个字符串,用于动态设置实例变量DetailsViewBuilderobject_id 是变量名的一部分。对象 ID 表示对象在内存中的位置。与直觉相反,对象的object_id 有时可能是负整数。这是否发生也可能与平台相关有关。这解释了为什么您只在生产中看到此错误。负的对象 ID 会导致实例变量名称变成类似于 @content_for_details_view_builder__-626960428 的名称,其中包含一个破折号。 破折号不允许作为 Ruby 中标识符的一部分。因此出现错误。

    您似乎在某处使用DetailsViewBuilder 的实例作为content_for 的参数,对吗?或者它可能间接发生在您正在使用的某个库中。

    无论如何,如果这是正确的行为,并且您只想避免实例变量名称错误,那么修复很简单。将 object_id 更改为 object_id.abs 以保证实例变量从不包含破折号

    def to_s
      "#{self.class.name.underscore}__#{object_id.abs}"
    end 
    

    【讨论】:

      【解决方案2】:

      在您发布的链接中建议使用caller(),实际上非常好。

      文档有一个不错的示例,但您似乎错过了这部分:

      可选的 start 参数确定要从结果中省略的初始堆栈条目数。

      基本上caller() 是您从未处理的异常中获得的堆栈跟踪的内容,只有您可以说它应该显示多少行。

      【讨论】:

        猜你喜欢
        • 2010-09-15
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 2015-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多