0.0
No commit activity in last 3 years
No release in over 3 years
A swiss army knife for collection of hashes. Play with it to know its strength.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.9
~> 10.0
>= 0
 Project Readme

Gem Version Bitdeli Badge

HashMultiTool

Installation

Add this line to your application's Gemfile:

gem 'hash_multi_tool'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_multi_tool

Usage

ARR_HASH = [{ foo: 'a', bar: 4 },
{ foo: 'b', bar: 3 },
{ foo: 'c', bar: 1 },
{ foo: 'b', bar: 4 }]
  1. HashMultiTool.sort_by_order ARR_HASH, [:bar, :foo] :-

    Note: By default it will sort in ASC order

    If you Want to sort in DESC order.

    HashMultiTool.sort_by_order ARR_HASH, [:bar, :foo], "DESC"

  2. Find array of hashes of matched key values :-

    HashMultiTool.select_by_key_value ARR_HASH, :bar, 4

    => [{ foo: 'a', bar: 4 }, { foo: 'b', bar: 4 }]

  3. Return all the available keys from a hash including subset hash :-

    hash = {"World" => "Region", "Country" => [{"Name" => "India", "Capital" => "Delhi"}, {"Name" => "England", "Capital" => "London"}]} hash_keys = HashMultiTool.collect_keys hash => ["World","Country","Name","Capital","Name","Capital"]

    hash_keys.uniq => ["World","Country","Name","Capital"]

  4. Merge the keys and with collected values belongs to respective keys and return a single hash

    arr = [{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2}]

    hash_keys = HashMultiTool.transpose_to_hash arr

    => {:a=>[1, 1, 1, 1], :b=>[2, 2, 2, 2], :c=>[3, 3, 3]}

  5. Return a collection of keys differentiated with values

    hash = {:a=>[1, 1, 1, 1], :b=>[2, 2, 2, 2], :c=>[3, 3, 3]}

    hash_keys = HashMultiTool.transpose_to_array hash

    => [{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2}]

  6. Sort a hash by value in ascending order:

    hash = {a: 1000, b: 500, c: 1500}

    HashMultiTool.sort_asc hash

    => {:a=>500, :b=>1000, :c=>1500}

  7. Sort a hash by value in descending order:

    hash = {a: 1000, b: 500, c: 1500}

    HashMultiTool.sort_desc hash

    => {:c=>1500, :b=>1000, :a=>500}

  8. Convert values of hash to percentage based on sum of values

    hash = {a: 20, b: 20}

    HashMultiTool.values_to_percentage hash

    => {:a=>50.0, :b=>50.0}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/hash_multi_tool/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 a new Pull Request