One liner nested hash creation in Ruby? (I come from Perl)
Unfortunately, there is no simple, practical way. A Ruby equivalent would be an ugly, ugly beast like:
((((@date[month] ||= {})[day] ||= {})[hours] ||= {})[min] ||= {})[sec] = 1
There is a way to assign default values for missing keys in hashes, though:
@date = Hash.new { |hash, key| hash[key] = {} }
# @date[:month] is set to a new, empty hash because the key is missing.
@date[:month][:day] = 1
Unfortunately this does not work recursively.
...unless you create it yourself; hooray for Ruby!
class Hash
def self.recursive
new { |hash, key| hash[key] = recursive }
end
end
@date = Hash.recursive
@date[month][day][hours][min][sec] = 1
# @date now equals {month=>{day=>{hours=>{min=>{sec=>1}}}}}
Keep in mind, though, that all unset values are now {}
rather than nil
.
->f{f[f]}[->f{Hash.new{|h,k|h[k]=f[f]}}]
Obviously.
Here's a couple of options similar to the answer given by @molf but without the monkey patch.
Using a factory method:
def hash_tree
Hash.new do |hash, key|
hash[key] = hash_tree
end
end
@date = hash_tree
@date[month][day][hours][min][sec] = 1
With a custom class:
class HashTree < Hash
def initialize
super do |hash, key|
hash[key] = HashTree.new
end
end
end
@date = HashTree.new
@date[month][day][hours][min][sec] = 1
Compared to the lambda expression given above, this is simpler and also in one line:
Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) }