Erb <% vs <%=

I was reading the erb section of Progamming Ruby this morning. WRT <
%, I note that they say, “Insert the given Ruby code at this point in
the generated program. If it outputs anything include this output in
the result.”

The last sentence left me a bit confused. Surely I can’t do something
like:

<% print(‘Hello, world!’) %>

I did some Googling and came across:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/2771

which seemed to say that’s exactly what I could do. However, of
course when I tried it, it didn’t work. That’s not too surprising to
me since it was always my understanding that ‘print’ directed output
to the console and not to standard out.

Anyway, can someone clear the confusion for me. If there is a way to
use ‘print’ as is suggested (or something similar), I’d sure like to
know about it as it would come in handy every once in a while.

Thanks for any input.

    ... doug

<% print(“hello world!”) %> will print to the console window that your
server is running in.

<%= “Hello World” %> Will print to the browser.

print in the above statement should print whatever argument you pass in
it
to the browser AND to the console.

<%= is the output block, and <% is just an evaluation block.

On Dec 19, 2007 10:52 AM, doug [email protected] wrote:

use ‘print’ as is suggested (or something similar), I’d sure like to
know about it as it would come in handy every once in a while.

Thanks for any input.

   ... doug


Ryan B.

<%= is the output block, and <% is just an evaluation block.

Yes. That’s the way I have always understood it. So, if that’s the
case, WRT ‘<%’, what does the sentence, “If it outputs anything,
include this output in the result” in Programming Ruby mean?

Thanks for the input.

   ... doug

The wording in the book is confusing you because you are only thinking
about erb in the context of RoR.

Here is a slightly more detailed explanation:

<% print ‘foo’ %> prints ‘foo’ to stdout connected to the process that
is interpreting the erb, but does not get included in the output
generated by the template.

<%= ‘foo’ %> inserts ‘foo’ into the output generated by template. Where
this output ends up depends on what the application interpreting the erb
does with the output, which in the case of rails is the browser.
However, an application could write it to a file, print it to stderr,
etc.

For example:

require 'erb'
template = ERB.new "The value is: <% print 'foo' %>"
puts template.result

results in “fooThe value is:” because foo is printed to stdout
immediately, but the results are stored then printed to stdout via puts.

require 'erb'
template = ERB.new "The value is: <%= 'foo' %>"
puts template.result

results in “The value is: foo” because foo is inserted into the output
from the template then printed to stdout via puts.

HTH,
-Bill

It’s saying that if whatever is contained within <%= and %> outputs
anything
at all, that output will be displayed on the page.

Say I had a method called funky_stuff in the application_helper:

def funky_stuff
BRIGHT GREENBRIGHT PINK
end

To use this method I would use <%= funky_stuff %> if I did <%
funky_stuff %>
the output would be supressed.

On Dec 19, 2007 11:11 AM, doug [email protected] wrote:


Ryan B.

The wording in the book is confusing you because you are only
thinking about erb in the context of RoR.

OK. I get it now. It did have me scratching my head. Thanks for the
input.

   ... doug

Try this again minus the bad formatting :slight_smile:

The wording in the book is confusing you because you are only thinking
about erb in the context of RoR.

Here is a slightly more detailed explanation:

<% print ‘foo’ %> prints ‘foo’ to stdout connected to the process that
is interpreting the erb, but does not get included in the output
generated by the template.

<%= ‘foo’ %> inserts ‘foo’ into the output generated by template. Where
this output ends up depends on what the application interpreting the erb
does with the output, which in the case of rails is the browser.
However, an application could write it to a file, print it to stderr,
etc.

For example:

require 'erb'
template = ERB.new "The value is: <% print 'foo' %>"
puts template.result

results in “fooThe value is:” because foo is printed to stdout
immediately, but the results are stored then printed to stdout via puts.

require 'erb'
template = ERB.new "The value is: <%= 'foo' %>"
puts template.result

results in “The value is: foo” because foo is inserted into the output
from the template then printed to stdout via puts.

HTH,
-Bill