# Cleaner way to write this

**URL:** <https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058>\
**Category:** Rails\
**Created:** [August 23, 2006, 2:52am UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058 "2006-08-23T02:52:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 2:52am UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/1 "2006-08-23T02:52:43Z")

</div>

Within the last few days I saw an excellent example of this but can’t  
find the link. So… I thought I’d ask the experts.

In my addressbook model, I wanted to be able to just say  
thing.full\_address to return a preformatted version of the customers  
entire address. Something like this:

# from addressbook model

def full\_address  
full = first\_name + ’ ’ + last\_name + ‘  
’ + address1 + ‘  
’  
unless address2.blank?; full += address2 + “  
”; end  
full += city + ’ ’ + state + ‘,’ + zip  
full  
end

I’m sure there’s a cleaner more compact way to do this, but this is the  
extent of my ruby knowledge at this point.

Any help cleaning / compacting this would be greatly appriciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 3:56am UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/2 "2006-08-23T03:56:21Z")

</div>

On Aug 22, 2006, at 5:52 PM, David C. wrote:

> unless address2.blank?; full += address2 + “  
> ”; end  
> full += city + ’ ’ + state + ‘,’ + zip  
> full  
> end
> 
> I’m sure there’s a cleaner more compact way to do this, but this is  
> the  
> extent of my ruby knowledge at this point.
> 
> Any help cleaning / compacting this would be greatly appriciated.

Don’t put view code (HTML) in your models!

Better to put that in a helper.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 4:06am UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/3 "2006-08-23T04:06:22Z")

</div>

Tom M. wrote:

> > def full\_address
> > 
> > Any help cleaning / compacting this would be greatly appriciated.
> 
> Don’t put view code (HTML) in your models!
> 
> Better to put that in a helper.
> 
> –  
> – Tom M.

I’m with Tom on this one.  
Make a partial that properly formats the address when passed the  
object.

FWIW, the format I think you were looking for is like this…

def print\_stuff  
“#{variable1}-#{variable2}”  
end

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/4 "2006-08-23T15:08:27Z")

</div>

Gareth A. wrote:

> def full\_address  
> [first\_name + ’ ’ + last\_name,  
> address1,  
> address2,  
> city + ’ ’ + state + ‘,’ + zip].compact # compact removes nil  
> values from an array  
> end
> 
> and then a helper
> 
> def format\_address(object)  
> object.respond\_to?(“full\_address”) and  
> object.full\_address.join("  
> ")  
> end
> 
> Gareth

Nice. Thanks Gareth!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/5 "2006-08-23T12:35:08Z")

</div>

On 8/23/06, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # from addressbook model
> 
> Any help cleaning / compacting this would be greatly appriciated.
> 
> Thanks!

One thing I’d suggest is writing a function in the model like this:

def full\_address  
[first\_name + ’ ’ + last\_name,  
address1,  
address2,  
city + ’ ’ + state + ‘,’ + zip].compact # compact removes nil  
values from an array  
end

and then a helper

def format\_address(object)  
object.respond\_to?(“full\_address”) and  
object.full\_address.join(“  
”)  
end

Gareth

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/6 "2006-08-23T16:05:53Z")

</div>

FYI, Added the delete\_if to get rid of the blank ones:

def full\_address  
addr = [first\_name + ’ ’ + last\_name,  
address1,  
address2,  
city + ’ ’ + state + ', ’ + zip].compact  
addr.delete\_if {|x| x.blank? }  
end

thx.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/cleaner-way-to-write-this/66058/7 "2006-08-23T15:35:32Z")

</div>

Gareth A. wrote:

> def full\_address  
> [first\_name + ’ ’ + last\_name,  
> address1,  
> address2,  
> city + ’ ’ + state + ‘,’ + zip].compact # compact removes nil  
> values from an array  
> end

Almost worked. address2.nil? returns false, but address2.blank? returns  
true. Since address2 isn’t nil, it still shows.

Thanks for pointing me in the right direction!!
