Git Product home page Git Product logo

hash_subtraction's Introduction

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

hash_subtraction's People

Contributors

kellymahan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.