I've been subscribed to Rubyflow for a while now, and it yields a few gems (sorry) every now and then. Today is Dan Watsons' quick post on setting a default value for an empty hash key value:
order = hash.fetch(:order, :desc)
And of course, the comments give us another great tip on setting the default value accross the entire Hash:
h = {} ; h.defaut('my_default_value')
Which can then be shorthanded again in the Hash.new initialiser:
irb(main):003:0> x = Hash.new('my_default_value')
=> {}
irb(main):004:0> x.keys
=> []
irb(main):005:0> x['no_keys_but_default_value']
=> "my_default_value"
Handy indeed.