ruby here-doc带循环

时间:2010-11-27 22:36:51

标签: ruby loops heredoc

你能做一个循环和here-doc,像这样:

array.each do |ele|
  a=<<-TEXT
   ele
   some stuff
  TEXT
end

由于

1 个答案:

答案 0 :(得分:6)

array = %w[one two many]

array.each do |ele|
  a=<<-TEXT
  This is some text and
  this --> #{ele} <-- is the ele!

  TEXT

  puts a
end

结果

This is some text and
this --> one <-- is the ele!

This is some text and
this --> two <-- is the ele!

This is some text and
this --> many <-- is the ele!