Forum: Ruby on Rails Hash.new{|h,k| h[k] = ""} ... but for views?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A9d77f57187ac17ae968d8bd2186d4ad?d=identicon&s=25 Don Walker (walkins)
on 2006-01-17 18:48
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?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-17 18:55
(Received via mailing list)
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.
<snip>
> 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...
A9d77f57187ac17ae968d8bd2186d4ad?d=identicon&s=25 Don Walker (walkins)
on 2006-01-17 19:27
Alex Young 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 :-)
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-17 19:50
Don Walker wrote:
> Alex Young 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
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2006-01-17 20:22
(Received via mailing list)
>
> 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 Buckley
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-18 12:06
(Received via mailing list)
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.
This topic is locked and can not be replied to.