Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby here-doc with a loop

Can you do a loop and here-doc, something like this:

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

Thanks

like image 993
Matt Avatar asked Oct 14 '25 14:10

Matt


1 Answers

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

results in

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!
like image 128
lbz Avatar answered Oct 17 '25 05:10

lbz