# Best way to set default for an optional local in a partial?

**URL:** <https://www.ruby-forum.com/t/best-way-to-set-default-for-an-optional-local-in-a-partial/181838>\
**Category:** Rails\
**Created:** [January 19, 2010, 3:41am UTC](https://www.ruby-forum.com/t/best-way-to-set-default-for-an-optional-local-in-a-partial/181838 "2010-01-19T03:41:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 19, 2010, 3:41am UTC](https://www.ruby-forum.com/t/best-way-to-set-default-for-an-optional-local-in-a-partial/181838/1 "2010-01-19T03:41:50Z")

</div>

What’s the best way to set the default value of an optional local  
variable in a partial template?

In the past I’ve used

\<% foo = default\_value unless (defined? foo) %\>

But [ActionView::Base](http://api.rubyonrails.org/classes/ActionView/Base.html) says _not_  
to use (defined? foo) and instead use (local\_assigns.has\_key? :foo) thus

\<% foo = default\_value unless (local\_assigns.has\_key? :foo) %\>

Some have suggested

\<% foo ||= default\_value %\>

which has the benefit of compact syntax, but will override passed values  
of nil and false. Some have also suggested

\<% foo = default\_value if local\_assigns[:foo].nil? %\>

which will override the passed value nil.

Are there other pros and cons to consider here? Are any of these  
strictly preferred over the others

Thanks!

-Ben
