Ruby 未定义的局部变量

发布于 2024-11-11 14:11:31 字数 1134 浏览 3 评论 0原文

以下是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2024-11-18 14:11:31

该 ERB 模板看起来已损坏,这是由您的缩进引起的问题。您只需要修复中间部分:

  <% priorities.each do |priority| %>
    * <%= priority %>
  <% end %>

替代语法是在该行的最开头添加一个%。在您的情况下,您无意中添加了一些空格,导致 ERB 的该部分无效。

That ERB template looks mangled, a problem caused by your indentation. You just need to fix the middle:

  <% priorities.each do |priority| %>
    * <%= priority %>
  <% end %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文