# Puts if exists?

**URL:** <https://www.ruby-forum.com/t/puts-if-exists/85819>\
**Category:** Rails\
**Created:** [February 16, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819 "2007-02-16T16:23:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 16, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819/1 "2007-02-16T16:23:48Z")

</div>

i have a real simple one today…

here’s how it works.

i have a field called address2 that is in most cases, blank…

the rhtml that does this looks like:

\<%= place[:address1] %\>  
\<%= place[:address2] %\>  
\<%= place[:city]  
%\>, \<%= place[:state] %\> \<%= place[:zip] %\>

what i would like to do is only print address 2 if it exists…

otherwise, blank…

is there a class method that does this?

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 16, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819/2 "2007-02-16T16:31:20Z")

</div>

If place[:address2] is nil or empty string then \<%= place[:address2] %\>  
should work. But I just noticed those   
 tags… You’ll probably  
want to  
do something more like \<%= “  
#{place[:address2]}  
” unless  
place[:address2].blank? %\>. If you’ve got an aversion to putting the  
HTML up  
inside the Ruby there, you could write this out as block

\<% if place[:address2] -%\>  
  
\<%= place[:address2] %\>  
  
\<% end -%\>

but it’s so short I’d just stick with embedding the HTML in the  
[embedded]  
Ruby.

RSL

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 16, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819/3 "2007-02-16T16:49:23Z")

</div>

a helper would be nice for something like this.

def address\_block()  
address = []  
address \<\< block[:address1]  
address \<\< block[:address2] unless block[:address2].blank?  
address \<\< block[:city] + “, " + block[:state] + " " + block[:zip]  
address.join(”  
")  
end

then in view:

\<%= address\_block(place) -%\>

now your view is nice and clean

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 16, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819/4 "2007-02-16T16:55:40Z")

</div>

Chris H. wrote:

> a helper would be nice for something like this.
> 
> def address\_block()  
> address = []  
> address \<\< block[:address1]  
> address \<\< block[:address2] unless block[:address2].blank?  
> address \<\< block[:city] + “, " + block[:state] + " " + block[:zip]  
> address.join(”  
> ")  
> end
> 
> then in view:
> 
> \<%= address\_block(place) -%\>
> 
> now your view is nice and clean

very nice…

i put all these into my code, and it worked!

thanks, all!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/puts-if-exists/85819/5 "2007-09-25T23:10:23Z")

</div>

should have been

def address\_block(block)
