# Erb \<% vs \<%=

**URL:** <https://www.ruby-forum.com/t/erb-vs/123597>\
**Category:** Rails\
**Created:** [December 19, 2007, 1:22am UTC](https://www.ruby-forum.com/t/erb-vs/123597 "2007-12-19T01:22:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 1:22am UTC](https://www.ruby-forum.com/t/erb-vs/123597/1 "2007-12-19T01:22:31Z")

</div>

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](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

```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 1:26am UTC](https://www.ruby-forum.com/t/erb-vs/123597/2 "2007-12-19T01:26:01Z")

</div>

\<% 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 1:42am UTC](https://www.ruby-forum.com/t/erb-vs/123597/3 "2007-12-19T01:42:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 3:35am UTC](https://www.ruby-forum.com/t/erb-vs/123597/4 "2007-12-19T03:35:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 1:45am UTC](https://www.ruby-forum.com/t/erb-vs/123597/5 "2007-12-19T01:45:53Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 3:53am UTC](https://www.ruby-forum.com/t/erb-vs/123597/6 "2007-12-19T03:53:19Z")

</div>

> 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
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 19, 2007, 3:47am UTC](https://www.ruby-forum.com/t/erb-vs/123597/7 "2007-12-19T03:47:29Z")

</div>

Try this again minus the bad formatting 🙂

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
