Forum: Ruby while loop in a here documents

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
equinox (Guest)
on 2008-12-02 03:35
(Received via mailing list)
I have a here documents of something like this:

form = <<"DONE"

while( bla ..bla .. bla) do

this

DONE


How can I do a while loop inside a here documents in ruby? Is it
possible?
Ken B. (Guest)
on 2008-12-02 04:40
(Received via mailing list)
On Mon, 01 Dec 2008 17:33:06 -0800, equinox wrote:

>
> How can I do a while loop inside a here documents in ruby? Is it
> possible?

If you want to use erb, then you can do something like that.

require 'erb'

erb=ERB.new <<-'DONE'
<% 5.times do %>
this text is repeated
<% end %>
DONE
form=erb.result(binding)

Be warned that you have to place the commands correctly to avoid getting
extra newlines. (This is mostly intended for HTML templating, where
newlines don't matter.)
equinox (Guest)
on 2008-12-02 05:20
(Received via mailing list)
On Dec 1, 7:08 pm, Ken B. <removed_email_address@domain.invalid> wrote:
>
> <% end %>
> DONE
> form=erb.result(binding)
>
> Be warned that you have to place the commands correctly to avoid getting
> extra newlines. (This is mostly intended for HTML templating, where
> newlines don't matter.)
>
> --
> Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
> Department of Computer Science. Illinois Institute of 
Technology.http://www.iit.edu/~kbloom1/

I am not using erb unfortunately, anyway to go around this?
Victor G. (Guest)
on 2008-12-02 09:32
(Received via mailing list)
Something like this?  Though this has other problems...
STDOUT.sync = true
my_text =  <<-FINISHED
Note the sequence of when these things are written.
#{3.times do
  puts "Hello"
end
} times a greeting was written.
end of my text
FINISHED
puts my_text
Robert K. (Guest)
on 2008-12-02 09:36
(Received via mailing list)
On 02.12.2008 02:33, equinox wrote:
>
> How can I do a while loop inside a here documents in ruby? Is it
> possible?

What exactly are you trying to achieve?  Do you want to have repeated
content from the here doc?

  robert
Ken B. (Guest)
on 2008-12-02 16:40
(Received via mailing list)
On Mon, 01 Dec 2008 19:15:29 -0800, equinox wrote:

>> > DONE
>> this text is repeated
>> Department of Computer Science. Illinois Institute of
>> Technology.http://www.iit.edu/~kbloom1/
>
> I am not using erb unfortunately, anyway to go around this?

No. You'll have to put the loop outside the string.
This topic is locked and can not be replied to.