Symbolized
Symbolized provides a Hash with indifferent access, but with keys stored internally as symbols.
This is particularly useful when you have a very big amount of hashes that share the same keys,
and it may become inefficient to keep all these identical keys as strings. An example of this
case is when you have data processing pipelines that process millions of hashes with the same
keys.
Installation
You can either install it manually:
% [sudo] gem install symbolized
Or include it in your Gemfile:
gem 'symbolized'
And then run bundle install
.
Usage
require 'symbolized'
symbolized_hash = SymbolizedHash.new
symbolized_hash['a'] = 'b'
symbolized_hash['a']
symbolized_hash[:a]
symbolized_hash.keys
symbolized_hash = SymbolizedHash.new({'a' => 'b'})
symbolized_hash['a']
symbolized_hash[:a]
symbolized_hash.keys
h = { 'a' => 'b' }
h['a']
h[:a]
h.keys
symbolized_hash = h.to_symbolized_hash
symbolized_hash['a']
symbolized_hash[:a]
symbolized_hash.keys
The gem provides almost the same methods and functionality provided by ActiveSupport's HashWithIndifferentAccess
, while storing keys internally as Symbols.
ActiveSupport
Compatibility
This gem is built with intent to be as much as possible compatible with ActiveSupport. You can include both Symbolized
and ActiveSupport
, and you are guaranteed to get ActiveSupport functionality and core extensions, and still have Symbolized
core extension and class.
Testing
Checkout travis.yml to see which Ruby versions the gem has been tested against. Alternatively, if you want to test it yourself, you can clone the repo, run bundle install
and then run rake test
.
Suggestions, Discussions and Issues
Please propose suggestions, open discussions, or report bugs and issues here.
Contributing
- Fork the repo
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Credits
The current code of this gem is heavily based on ActiveSupport 4.2 HashWithIndifferentAccess. Some parts are direct clones, other parts have been modified and/or refactored.
License
Copyright (c) 2015 Tamer Shlash (@TamerShlash). Released under the MIT License.