Keypath extensions to Ruby's collection classes.
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

keypath-based collection access extensions for Ruby.

Build Status Code Climate Test Coverage Dependency Status

This gem allows you to access nested Ruby collections (Hash, Array, etc) using keypaths.

For example, say you had a nested data structure like:

data = {
    :item_one => {:id => 1, :url => ''},
    :something_else => [
            :id => 1,
            :url => ''

You could access "" through: something_else.0.url. Basically, this is intended to allow you to manipulate/transform large nested structures that you might get back from a JSON document.


keypath-ruby is on RubyGems as key_path. But you can also add the repo to your Gemfile:

gem 'key_path', :git => ''


KeyPath is at least two things. First, it's a class (actually, KeyPath::Path) which represents a path (this is just a string, and has methods to go back and forth from it) and secondly a set of class extensions for Enumerable, Hash and String which allow you to use the native collection classes with keypaths.

require 'key_path'

data = {
    :item => {
        :url => ''

# fetching a path
path = 'item.url'
data.value_at_keypath(path) #=> ''

# finding all `:url` paths in a collection
data.keypaths_for_nested_key(:url) #=> {item.url => ''}

# going back and forth from a string
path.to_s #=> 'item.url'
'item.url'.to_keypath #=> #<KeyPath:70096895112220 path=item.url>

# get the parent of a keypath (or nil, if there isn't one)
path.parent #=> #<KeyPath:70096895221110 path=item>

# setting a path
data.set_keypath(path, '')


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request


Copyright (c) 2013 Nick Charlton (