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


#1

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?


#2

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 :slight_smile:


#3

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…


#4

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 :slight_smile:

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


#5

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.


#6

Don Walker wrote:

<% favorite_food = ‘’ unless defined?(favorite_food) %>

… but as you noted it is definitely NOT slick :slight_smile:

Oops - yes, that’s what I was after :slight_smile:

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.