https://github.com/samirhodzic/multi_key_hash
Multiple key hash for ruby
https://github.com/samirhodzic/multi_key_hash
hash ruby ruby-gem ruby-on-rails
Last synced: about 1 year ago
JSON representation
Multiple key hash for ruby
- Host: GitHub
- URL: https://github.com/samirhodzic/multi_key_hash
- Owner: SamirHodzic
- License: mit
- Created: 2017-12-23T15:35:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-23T16:39:24.000Z (over 8 years ago)
- Last Synced: 2024-04-25T06:20:51.871Z (about 2 years ago)
- Topics: hash, ruby, ruby-gem, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 6.84 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MultiKeyHash
> Hash with multiple keys for same value, symbol/string-indifferent key access.
[](https://badge.fury.io/rb/multi_key_hash)
[![build-url][build-url-svg]][build-url]
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'multi_key_hash'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install multi_key_hash
## Usage
#### Creating new multiple keys hash and accessing it's keys
```ruby
require 'multi_key_hash'
multi_hash = MultiKeyHash.new({%w[one two three] => 'number', %i[a b c] => 'letter', 'single' => 'value'})
multi_hash['one'] # number
multi_hash[:a] # letter
multi_hash['single'] # value
```
#### Modifying values inside hash
```ruby
multi_hash = MultiKeyHash.new({%w[one two three] => 'number', %i[a b c] => 'letter', 'single' => 'value'})
multi_hash['one'] = 'first_number' # first_number
multi_hash['two'] # number
multi_hash[:a] = 'first_letter' # first_letter
multi_hash[:b] # letter
```
#### Adding new keys to hash
```ruby
multi_hash = MultiKeyHash.new({%w[one two three] => 'number', %i[a b c] => 'letter', 'single' => 'value'})
multi_hash[:language] = 'english' # single key
multi_hash[[:ruby, :js, :php]] = 'programming_language' # multiple keys
```
#### Deleting keys from hash
```ruby
multi_hash = MultiKeyHash.new({%w[one two three] => 'number', %i[a b c] => 'letter', 'single' => 'value'})
multi_hash.delete(:a)
multi_hash[:b] #letter
multi_hash.delete('one')
multi_hash['two'] #number
```
#### Converting to regular hash
```ruby
multi_hash = MultiKeyHash.new({%w[one two three] => 'number', %i[a b c] => 'letter', 'single' => 'value'})
multi_hash.to_h
# {
# 'one' => 'number',
# 'two' => 'number',
# 'three' => 'number',
# :a => 'letter',
# :b => 'letter',
# :c => 'letter',
# 'single' => 'value'
# }
```
## License
[MIT License](https://opensource.org/licenses/MIT).
[build-url]: https://travis-ci.org/SamirHodzic/multi_key_hash
[build-url-svg]: https://travis-ci.org/SamirHodzic/multi_key_hash.svg?branch=master