Project

deep_slice

0.0
No commit activity in last 3 years
No release in over 3 years
Like `Hash.slice` from ActiveSupport, but allows for slicing nested keys.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 0.11
~> 12.2
~> 3.7
 Project Readme

deep_slice

Build Status

Like Hash.slice from ActiveSupport, but allows for slicing nested keys.

Installation

Add this line to your application's Gemfile:

gem 'deep_slice'

And then execute:

$ bundle

Or install it yourself as:

$ gem install deep_slice

Usage

Take the following hash as an example

h ={
  :a => 1,
  :b => { :c => 2, :d => 3, :z => -1 },
  :e => {
    :f => { :g => 4, :h => 5 }
  },
  :i => [
    {:j => 6, :k => 7},
    {:j => 8, :k => 9}
  ],
  :d => nil
}

Normal hash slicing works:

h.deep_slice(:a, :d)
  => {:a => 1, :d => nil}

You can slice to any depth:

h.deep_slice(:e => {:f => :g})
  => {:e => {:f => {:g => 4}}}

Arrays of hash keys can be specified at any depth:

h.deep_slice(:a, :b => [:c, :d])
  => {:a => 1, :b => {:c => 2, :d => 3}}

Keys can be sliced out of arrays at any depth:

h.deep_slice(:i => :k)
  => {:i => [{:k => 7}, {:k => 9}]}

Contributing

  1. Fork it ( http://github.com//deep_slice/fork )
  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