DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Defaults For Hash Tables In Ruby

06.09.2010
| 2667 views |
  • submit to reddit
        // In Ruby you can define defaults for <b>Hash</b> objects using the <b>default</b> method. However, if you want your default to be a complex object such as an array, you need to apply the idiom as shown below.

hash_table = Hash.new { |hash, key| hash[key] = Array.new }