Diving into ruby hashes
Nested hashes in ruby are a PITA. if in somewhere along the way one of your keys are missing / returning an empty hash, you'll get a fancy:
undefined method `[]' for nil:NilClass (NoMethodError)
Yes. annoying.
I took a shot at this issue by creating this ruby hash spinoff that accepts default values to key fetch requests. here it is
class NestedHash < Hash
def [](value, default = NestedHash.new)
self.fetch(value, default)
end
def self.from_hash(other_hash)
nested_hash = NestedHash.new
other_hash.each_pair do |key, val|
if val.is_a?(Hash)
nested_hash[key] = NestedHash.from_hash(val)
else
nested_hash[key] = val
end
end
return nested_hash
end
end
# Setup a regular hash
h = NestedHash.new
h["elad"] = NestedHash.new
h["elad"]["adam"] = NestedHash.new
h["elad"]["adam"]["hungry"] = "always"
# Test non existing value
puts h["elad"]["miki"]["hungry", "no"] # => "no"
# Test Converstions from regular hashes
real_hash = {:elad => {:koko => :awesome}, :adam => {:moko => :no}}
nested = NestedHash.from_hash(real_hash)
# Missing Value
puts nested[:elad][:invalid_key, "not here"] # => "not here"
# Existing value
puts nested[:elad][:koko] # => "awesome"
also available as a gist: https://gist.github.com/eladmeidar/5857066
Written by Elad Meidar
Related protips
2 Responses
only this causes a rather crazy situations where
h["elad"]["adam"]["hungry"] = "always"
h["elad"]["adam"]["hungry"] # => nil
another approach would be:
class NestedHash < Hash
def initialize(*args)
inner_hash = lambda {|h,k| h[k] = Hash.new &inner_hash }
super(*args, &inner_hash)
end
end
over 1 year ago
·
or use fetch
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#