0.0
No commit activity in last 3 years
No release in over 3 years
Is there a way to blindly assign nested values to a Ruby hash without creating each key’s hash separately? Yes, but it’s more involved than you’d think.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.10
~> 10.0
>= 0
 Project Readme

BottomlessHash

firedev.com/2015/bottomless-ruby-hash firedev.com/posts/2015/making-bottomless-hash-ruby-gem

BottomlessHash is a subclass or Ruby Hash that allows you to blindly assign values without raising errors on missing keys. So you can do chained assings and accessing keys without checking or using .try.try.try....

params = BottomlessHash.new
params[:world][:thailand][:bangkok][:bangna]

params
=> {:world=>{:thailand=>{:bangkok=>{:bangna=>{}}}}}

params[:missing][:key]
=> {} # no error raised

Caveat

It returns an empty hash instead of nil if there is no value. Keep that in mind:

BottomlessHash.new[:missing][:key]
=> {}

Hash.bottomless

It also adds #bottomless method to Ruby Hash Class so now you can have as save version of your hash:

yaml.bottomless[:see][:if][:there][:is][:a][:value][:here]
=> {} # apparently there is not

Installation

Add this line to your application's Gemfile:

gem 'bottomless_hash'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bottomless_hash

License

The gem is available as open source under the terms of the MIT License.

firedev.com