Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Defaults For Hash Tables In Ruby

DZone's Guide to

Defaults For Hash Tables In Ruby

·
Free Resource
// In Ruby you can define defaults for Hash objects using the default 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 }
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}