# Hash.new{|h,k| h\[k\] = ""} ... but for views?

**URL:** <https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657>\
**Category:** Rails\
**Created:** [January 17, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657 "2006-01-17T18:48:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 17, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/1 "2006-01-17T18:48:09Z")

</div>

I sometimes buy myself a default value for a hash using something like  
this: h = Hash.new{|h,k| h[k] = “”}. I can now pass ‘h’ any key and if a  
value has not yet been associated with that key I receive an empty  
string.

I would love to do something similar in my partials so that I wouldn’t  
have to worry about passing in every variable that is referenced.

For example, if I have a partial ‘\_foo.rhtml’ as follows:

\<%= favorite\_food %\>  
\<%= favorite\_color %\>

… and I attempt to render it with a call of:

\<%= render(:partial =\> ‘foo’,  
:locals =\> {:favorite\_food =\> “Anything but natto.”}) %\>

… then I’ll be scolded since I didn’t provide a value for  
‘favorite\_color’. I’d rather just give all unassigned variables a  
default value as described in the first paragraph.

Anyone know of a way this can be done?

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 17, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/2 "2006-01-17T19:27:37Z")

</div>

Alex Y. wrote:

> The first thing that springs to mind is a quick
> 
> \<% food\_color = ‘’ if food\_color.nil? -%\>
> 
> at the top. There may be a slicker way, but if there is it escapes  
> me…

Thanks, but this won’t work because ‘food\_color’ isn’t defined to call  
‘nil?’ upon. I could do what I think you intended with:

\<% favorite\_food = ‘’ unless defined?(favorite\_food) %\>

… but as you noted it is definitely NOT slick 🙂

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 17, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/3 "2006-01-17T18:55:31Z")

</div>

Don Walker wrote:

> I would love to do something similar in my partials so that I wouldn’t  
> have to worry about passing in every variable that is referenced.  
>   
> Anyone know of a way this can be done?

The first thing that springs to mind is a quick

\<% food\_color = ‘’ if food\_color.nil? -%\>

at the top. There may be a slicker way, but if there is it escapes  
me…

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 17, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/4 "2006-01-17T19:50:20Z")

</div>

Don Walker wrote:

> Alex Y. wrote:
> 
> > The first thing that springs to mind is a quick
> > 
> > \<% food\_color = ‘’ if food\_color.nil? -%\>
> > 
> > at the top. There may be a slicker way, but if there is it escapes  
> > me…
> 
> Thanks, but this won’t work because ‘food\_color’ isn’t defined to call  
> ‘nil?’ upon. I could do what I think you intended with:
> 
> \<% favorite\_food = ‘’ unless defined?(favorite\_food) %\>
> 
> … but as you noted it is definitely NOT slick 🙂

Couple of things here…

a = Hash.new("") should return an empty string if a undefined key is  
used.

I suppose you could set up an before filter for each of your view  
methods that sets default values for each of your instance variables.

\_Kevin

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 17, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/5 "2006-01-17T20:22:55Z")

</div>

> I would love to do something similar in my partials so that I wouldn’t  
> have to worry about passing in every variable that is referenced.

class Foo \< ActiveRecord::Base

alias\_method :old\_favorite\_food, :favorite\_food  
def favorite\_food  
self.old\_favorite\_food ||= “pizza”  
end

end

The above redefines the getter so it will never return nil.

-Brian B.

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [January 18, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/hash-new-h-k-h-k-but-for-views/52657/6 "2006-01-18T12:06:18Z")

</div>

Don Walker wrote:

> \<% favorite\_food = ‘’ unless defined?(favorite\_food) %\>
> 
> … but as you noted it is definitely NOT slick 🙂

Oops - yes, that’s what I was after 🙂

But a bit slicker is (and you can actually do this where you use the \<%=  
%\> tag, so the default is actually defined where you need it):

\<%= food\_color ||= ‘blue’ %\>

That’s a little smarter.
