我想从这里的文档(heredoc)中调用一个块,但我似乎无法让它工作。我是否误解了语法,或者这真的不可能(使用 Ruby 1.9.3)?
以下示例代码说明了我正在尝试做的事情。
colors = { "red" => "#FF0000",
"orange" => "#FF7F00",
"yellow" => "#FFFF00",
"green" => "#00FF00",
"blue" => "#0000FF",
"indigo" => "#4B0082",
"violet" => "#8F00FF" }
puts
puts <<COLORS
--------------------------------------------------------------------------------
The colors of the rainbow:
#{ colors.each { |key, value| puts "#{key} (#{value})" } }
--------------------------------------------------------------------------------
COLORS
这会在我的系统上产生以下输出。
red (#FF0000)
orange (#FF7F00)
yellow (#FFFF00)
green (#00FF00)
blue (#0000FF)
indigo (#4B0082)
violet (#8F00FF)
--------------------------------------------------------------------------------
The colors of the rainbow:
{"red"=>"#FF0000", "orange"=>"#FF7F00", "yellow"=>"#FFFF00", "green"=>"#00FF00", "blue"=>"#0000FF", "indigo"=>"#4B0082", "violet"=>"#8F00FF"}
--------------------------------------------------------------------------------
但是,我期待以下内容。
--------------------------------------------------------------------------------
The colors of the rainbow:
red (#FF0000)
orange (#FF7F00)
yellow (#FFFF00)
green (#00FF00)
blue (#0000FF)
indigo (#4B0082)
violet (#8F00FF)
--------------------------------------------------------------------------------