# The RoR equivalent of out.write() in JSP?

**URL:** <https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861>\
**Category:** Rails\
**Created:** [March 14, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861 "2006-03-14T21:09:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/1 "2006-03-14T21:09:36Z")

</div>

All,

In JSP, I can output strings in the Web page by either

\<%= foo %\> //foo is a string or returns a string  
or  
\<% out.write(“test”) %\> //write directly to the output stream.

What is the method of “writing to the output stream” in RoR? Basically,  
what is the equivalent of out.write()?

I have an if then statement that I want to put around a call to

h some\_stuff

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/2 "2006-03-14T21:21:53Z")

</div>

Not entirely sure what out.write() does (just outputs to the page, I  
guess?). If you want to conditionally display something though, do

\<%= h some\_stuff if some\_cond? %\>

If it’s a bit more complex you can do  
\<% if some\_cond? %\>  
\<%= h some\_stuff %\>  
\<%= h some\_other\_stuff %\>  
\<% end %\>

\<%= expression %\> writes the result of expression to the page.

Pat

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/3 "2006-03-14T21:51:00Z")

</div>

I’m sure he knows what \<%= expression %\> does since he mentioned it  
first 🙂

I don’t have an answer Wes, but I’ve wished for the same thing.  
Sometimes it is just aesthetically nicer to use something like  
out.write() in a \<% %\> code block. Perhaps it affects people who use  
color coded editors more…

On 3/14/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Dav Y.  
[http://AkuAku.org/](http://AkuAku.org/)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/4 "2006-03-14T22:12:21Z")

</div>

Wes G. wrote:

> Yeah, it just seems so cheesy to me to do
> 
> \<% if %\>  
> \<%= stuff %\>  
> \<% else %\>  
> \<%= other stuff %\>  
> \<% end %\>

Try:

\<%= if some\_condition  
some\_code\_evaluating\_to\_a\_string  
else  
some\_other\_string  
end %\>

That works because if actually returns a value…

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/5 "2006-03-14T22:30:09Z")

</div>

Alex,

Thanks - that makes a lot of sense.

🙂

Wes

Alex Y. wrote:

> Wes G. wrote:
> 
> > Yeah, it just seems so cheesy to me to do
> > 
> > \<% if %\>  
> > \<%= stuff %\>  
> > \<% else %\>  
> > \<%= other stuff %\>  
> > \<% end %\>
> 
> Try:
> 
> \<%= if some\_condition  
> some\_code\_evaluating\_to\_a\_string  
> else  
> some\_other\_string  
> end %\>
> 
> That works because if actually returns a value…

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/6 "2006-03-14T22:56:29Z")

</div>

On Mar 14, 2006, at 12:09 PM, Wes G. wrote:

> what is the equivalent of out.write()?  
> you can use two ways to do this:

\<% \_erbout \<\< “foo” %\>  
OR  
\<% concat(“foo”, binding) %\>

Neither of which is very pretty 😉

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/7 "2006-03-14T23:02:07Z")

</div>

Conditional expressions work:

\<%= (1==1) ? ‘yep’ : ‘nope’ %\>  
\<%= (1==2) ? ‘yep’ : ‘nope’ %\>

Joe

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 15, 2006, 2:21am UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/8 "2006-03-15T02:21:55Z")

</div>

On Tue, 14 Mar 2006 21:09:36 +0100, Wes G. wrote:

> What is the method of “writing to the output stream” in RoR? Basically,  
> what is the equivalent of out.write()?
> 
> I have an if then statement that I want to put around a call to

Usually, when I’ve wanted to do that, I realized that the view was  
actually  
getting too complicated, and the code really belonged in either the  
model  
or a helper… I’m not saying that’s always the case, but it’s something  
to  
consider.

Jay L.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 14, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/the-ror-equivalent-of-out-write-in-jsp/81861/9 "2006-03-14T22:02:25Z")

</div>

Yeah, it just seems so cheesy to me to do

\<% if %\>  
\<%= stuff %\>  
\<% else %\>  
\<%= other stuff %\>  
\<% end %\>

especially since we _know_ there _must_ be some kind of handle to the  
output stream anyway.

Actually, maybe not at the Ruby level - I don’t understand the IO model  
for Ruby yet, so I can’t say. But it seems like there should be some  
suitable abstraction, no?

Anyhow, cheese is ok for now 🙂

Wes

Dav Y. wrote:

> I’m sure he knows what \<%= expression %\> does since he mentioned it  
> first 🙂
> 
> I don’t have an answer Wes, but I’ve wished for the same thing.  
> Sometimes it is just aesthetically nicer to use something like  
> out.write() in a \<% %\> code block. Perhaps it affects people who use  
> color coded editors more…
> 
> On 3/14/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > 
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Dav Y.  
> [http://AkuAku.org/](http://AkuAku.org/)
