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

<%= 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

Ryan Bigg wrote:

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

Ryan Bigg wrote:

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