https://github.com/odlp/dotize
Dig into a Hash with a dot-delimited string
https://github.com/odlp/dotize
Last synced: 11 months ago
JSON representation
Dig into a Hash with a dot-delimited string
- Host: GitHub
- URL: https://github.com/odlp/dotize
- Owner: odlp
- Created: 2016-01-09T17:42:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-04-04T16:19:40.000Z (about 8 years ago)
- Last Synced: 2025-06-17T06:50:20.312Z (12 months ago)
- Language: Ruby
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dotize
[](https://travis-ci.org/odlp/dotize)
Access values from a deeply-nested Hash using a simple string:
```ruby
my_hash = {'a' => {'b' => {'c' => 123}}}
my_hash.extend(Dotize)
my_hash.dot('a.b.c') # => 123
```
If a value isn't found, nil is returned by default:
```ruby
my_hash.dot('a.b.z.z.z') # => nil
```
You can provide a block to override the default, like Ruby's [Hash#fetch](http://ruby-doc.org/core-2.3.0/Hash.html#method-i-fetch):
```ruby
my_hash.dot('a.b.z.z.z') { |el| 2 + 2 } # => 4
```
## Development
- Run specs via `rake` or `bundle exec rspec`
## Credits
The name of this is ~~inspired-by~~ copied from [github.com/vardars/dotize](https://github.com/vardars/dotize), a Javascript equivalent.