0

当尝试将跟踪输出添加到 ERB 模板时,我在尝试查找变量ex_title的来源时遇到了以下问题。

试图列出当前范围内所有可能的变量,但根本不存在:

<%= instance_variables.grep(/ex_title/) %>  # renders an empty array
<%= global_variables.grep(/ex_title/) %>    # renders an empty array
<%= local_variables.grep(/ex_title/) %>     # renders an empty array

<%= instance_variables.sort %>   # renders array with many elements
<%= global_variables.sort %>     # renders array with many elements
<%= local_variables.sort %>      # renders array with many elements

ex_title变量可证明确实存在:

<%= ex_title %>                  # renders "Categories - Online store"
<%= ex_title.class %>            # renders "String"
<%= ex_title.object_id %>        # renders "15825900"

如果不在全局、局部或实例变量中,还能在哪里找到?

4

1 回答 1

0

那么你必须断定它是一种方法。用于defined?确定是什么ex_title。例如:

defined? ex_title  #=> nil

def ex_title; end
defined? ex_title  #=> "method"

ex_title = Object.new
defined? ex_title  #=> "local-variable"
于 2013-02-16T14:24:24.330 回答