Newbie erb question

Hi,

I’m trying to use some erb templates to generate some code with ruby. It’s
not going too well - in my template, I have something like:

…code template in plain text…
<% code_lines.each { |line| print line } %>
…more plain text code…

I try to capture the generated code inside my ruby script like this:

File.open(‘C:\Templates\MyCodeTemplate.erb’) { |fh|
erb = ERB:new(fh.read)
gen_code = erb.result(binding)

Just to see the generated output.

puts gen_code
}

I would like the lines in code_lines to be returned to the calling ruby code
with the rest of the template code, but instead it goes to stdout! So all my
actual code lines are dumped directly to screen instead of being included in
the result being returned to the gen_code variable.

What am I doing wrong? Is there a simple tutorial for ERB in english
somewhere? I’m using ruby 1.8.1 with erb 2.0.4.

Thanks a lot!

···


“If it was so, it might be; and if it were so, it would be; but as it isn’t,
it ain’t. That’s logic” – Lewis Carroll

I’m trying to use some erb templates to generate some code with ruby. It’s
not going too well - in my template, I have something like:

…code template in plain text…
<% code_lines.each { |line| print line } %>
…more plain text code…

Try this:

<% code_lines.each do |line| %>
<%= line %>
<% end %>

What am I doing wrong? Is there a simple tutorial for ERB in english
somewhere? I’m using ruby 1.8.1 with erb 2.0.4.

I read the source code to Why’s Poignant Guide to figure out how to use ERB.
http://rubyforge.org/cgi-bin/viewcvs/cgi/viewcvs.cgi/?cvsroot=poignant

···

At 03:14 AM 3/23/2004, Einar Buffer wrote:


Bret Pettichord, Software Tester
Consultant - www.pettichord.com
Author - www.testinglessons.com
Blogger - www.io.com/~wazmo/blog

Homebrew Automation Seminars
March 19, Redmond, Washington
April 30, Austin, Texas
www.pettichord.com/training.html

…code template in plain text…
<% code_lines.each { |line| print line } %>
…more plain text code…

Try this:

<% code_lines.each do |line| %>
<%= line %>
<% end %>

Alternatively, you can do:
<%= code_lines.join %>

Or, if your example was simplified and you really need to do something
with each line, then for example:

<%= code_lines.collect { |line| line.capitalize }.join %>

But it’s not as logical as could be. Wouldn’t it be possible to point
STDOUT somewhere else and insert it correctly? Perhaps ERb/eRuby
already allows for this and we just don’t know :).

···


David Heinemeier Hansson,
http://rails.nextangle.com/ – Rails: Web-applications in Ruby
http://www.basecamphq.com/ – Web-based Project Management
http://www.loudthinking.com/ – Broadcasting Brain

“David Heinemeier Hansson” david@loudthinking.com wrote in message
news:28220EB3-7F2D-11D8-AC42-000A958E6254@loudthinking.com

…code template in plain text…
<% code_lines.each { |line| print line } %>
…more plain text code…

Try this:

<% code_lines.each do |line| %>
<%= line %>
<% end %>

Alternatively, you can do:
<%= code_lines.join %>

Or, if your example was simplified and you really need to do something
with each line, then for example:

<%= code_lines.collect { |line| line.capitalize }.join %>

Thanks, those are good tips that I’ll definitely use!

But it’s not as logical as could be. Wouldn’t it be possible to point
STDOUT somewhere else and insert it correctly? Perhaps ERb/eRuby
already allows for this and we just don’t know :).

Indeed, that would be a very nice feature! I’ve been looking around the Net
a bit for a bit in-depth english tutorials on ERb (and eRuby), but I haven’t
found any yet. Sadly, I don’t read japanese (like a lot of the world’s
myriad of languages… I read my native norwegian pretty well, but obviously
that doesn’t help much :-P)

Thanks for your help!

Regards,
Einar