No commit activity in last 3 years
No release in over 3 years
hash_subtraction is a gem that provides methods for different subtractive methods for hashes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 0
 Project Readme

hash_subtraction is a gem that provides methods for different subtractive methods for hashes.

Installation¶ ↑

In your Gemfile:

gem 'hash_subtraction'

Then run bundle install.

Usage¶ ↑

to make use of hash_subtraction you must first extend the hash instance you wish to call the methods on.

myhash = {
  a: 'a', 
  b: 'b',
	num: 5,
	array: [1,2,3]
}
myhash2 = {
  a: 'a', 
  b: 'c',
	num: 10,
	array: [1,3]
}
#extend the hash
myhash.extend(HashSubtraction)

#subtract the hash
myhash-myhash2
#=> {:b=>"b", :num=>-5, :array=>[2]}
#arrays and numbers are subtracted, strings are removed if the value is a duplicate. Otherwise it is left alone.

#remove duplicate key value pairs
myhash.remove_duplicates(myhash2)
#=> {:b=>"b", :num=>5, :array=>[1, 2, 3]}
#only duplicate key and value combinations are removed

#remove duplicate keys
myhash.remove_duplicate_keys(myhash2)
#=> {}
#all matching keys are removed

#find the difference between the 2 hashes
myhash.diff(myhash2)
#=> {:b=>"c", :num=>10, :array=>[1, 3]}
# myhash[:a] is the only key value thats the same so it is excluded.
# The values from myhash2 will be listed if they are different as well as any keys in myhash1 not in myhash2

Copyright © 2014 Kelly Mahan, released under the MIT license