Skip to content

firedev/bottomless_hash

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

Bottomless Hash Ruby Gem

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published