I found this new (to me) Ruby tidbit in a comment on a two year old blog post here.
Basically, in Ruby, you can initialize a new Hash so that any time you reference a given value, keyed by a given key, if that value is previously nil, it assigns a default value.
In “gooder” English, it basically just lets you assign a “default” value for items in a Hash.
Why is this useful? Well, let’s say for example that you are iterating over some array, building up a hash as you go. Maybe the key you are using for the hash is derived from a certain property of each of the objects in your array.
Concrete Example (Java)
We have an ArrayList of Product objects. Product has a “getCategory()” method, and we want to build up a HashMap whereby the key is the product category, and the value is an ArrayList of all Products that fall into a given category.
Map<String,List<Product>> map = new HashMap<String, List<Product>>();
for (Product product : products){
if ( map.get( product.getCategory() ) == null ){
map.put( product.getCategory(), new ArrayList<Product>() );
}
map.get( product.getCategory() ).add( product );
}
I still can’t get over how downright ugly Java code looks now if you want to get it to compile without warnings, thanks to generics. Just. Yuck.
Concrete Example (Ruby)
We have an Array of Product objects. Product has an attr_accessor for category. We want to build up a Hash object in the same manner as we did for the Java example above.
map = Hash.new { |h,k| h[k] = [] }
products.each { |product| map[product.category] << product }
You’ll note that I don’t have to do any check for null (or nil) because the initializer passed to Hash basically says “Any time someone grabs something from the hash, if it’s null, initialize it with a new Array”.



