2013-02-16 23 views
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" 

還能在哪裏,如果被發現不在全局,局部或實例變量中?

+1

也許這是一個幫助函數返回一個字符串。 – Matzi 2013-02-16 14:13:21

回答

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" 
+0

謝謝。界定?確實有助於揭示它最終是一種方法。 – 2013-02-16 15:16:04