Ruby 未定义的局部变量
以下是 ERB 教程中的代码。当我尝试执行代码时,编译器抱怨说“(erb):16: main:Object (NameError) 的未定义局部变量或方法‘优先级’”。我无法弄清楚原因。有人可以帮我吗?
require "erb"
# Create template.
template = %q{
From: James Edward Gray II <[email protected]>
To: <%= to %>
Subject: Addressing Needs
<%= to[/\w+/] %>:
Just wanted to send a quick note assuring that your needs are being
addressed.
I want you to know that my team will keep working on the issues,
especially:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
* <%= priority %>
% end
Thanks for your patience.
James Edward Gray II
}.gsub(/^ /, '')
message = ERB.new(template, 0, "%<>")
# Set up template data.
to = "Community Spokesman <spokesman@ruby_community.org>"
priorities = [ "Run Ruby Quiz",
"Document Modules",
"Answer Questions on Ruby Talk" ]
# Produce result.
email = message.result
puts email
The following is code from an ERB tutorial. When I tried to execute the code, the compiler complained saying "(erb):16: undefined local variable or method `priority' for main:Object (NameError)". I cannot figure out the reason. Could someone please help me out?
require "erb"
# Create template.
template = %q{
From: James Edward Gray II <[email protected]>
To: <%= to %>
Subject: Addressing Needs
<%= to[/\w+/] %>:
Just wanted to send a quick note assuring that your needs are being
addressed.
I want you to know that my team will keep working on the issues,
especially:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
* <%= priority %>
% end
Thanks for your patience.
James Edward Gray II
}.gsub(/^ /, '')
message = ERB.new(template, 0, "%<>")
# Set up template data.
to = "Community Spokesman <spokesman@ruby_community.org>"
priorities = [ "Run Ruby Quiz",
"Document Modules",
"Answer Questions on Ruby Talk" ]
# Produce result.
email = message.result
puts email
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 ERB 模板看起来已损坏,这是由您的缩进引起的问题。您只需要修复中间部分:
替代语法是在该行的最开头添加一个
%
。在您的情况下,您无意中添加了一些空格,导致 ERB 的该部分无效。That ERB template looks mangled, a problem caused by your indentation. You just need to fix the middle:
The alternate syntax is to have a
%
at the very beginning of the line. In your case you have inadvertently added some spaces which are rendering that part of the ERB invalid.