SixArm » Ruby » HashMore class for recursive hashing
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.yardoc
bin
coverage
doc
lib
test
.codeclimate.yml
.document
.gemtest
.gitattributes
.gitignore
.rspec
.travis.yml
CHANGES.md
CONTRIBUTING.md
Gemfile
Gemfile.lock
LICENSE.md
README.md
Rakefile
VERSION
sixarm_ruby_hash_more-1.3.2-sha512.txt
sixarm_ruby_hash_more-1.3.2.gem
sixarm_ruby_hash_more.gemspec

README.md

SixArm.com » Ruby »
HashMore class for dynamic hashes

Gem Version Build Status Code Climate Coverage Status

Introduction

HashMore is a hash of hashes of hashes and so on recursively, enabling arbitrarily access (and assignment of values) to specific elements of a (multi-dimensional) hash, whether the element already exists or not.

For docs go to http://sixarm.com/sixarm_ruby_hash_more/doc

Want to help? We're happy to get pull requests.

Install

To install using a Gemfile, add this:

gem "sixarm_ruby_hash_more", ">= 1.3.2", "< 2"

To install using the command line, run this:

gem install sixarm_ruby_hash_more -v ">= 1.3.2, < 2"

To install using the command line with high security, run this:

wget http://sixarm.com/sixarm.pem
gem cert --add sixarm.pem && gem sources --add http://sixarm.com
gem install sixarm_ruby_hash_more -v ">= 1.3.2, < 2" --trust-policy HighSecurity

To require the gem in your code:

require "sixarm_ruby_hash_more"

Details

Ruby's built-in Hash class will raise an error if code attempts to access an element which doesn't exist; HashMore will create the necessary elements and then execute the access.

Additionally, using the += operator to assign a numeric value to an element

Examples

hash = HashMore.new
hash["red"]["apple"] = true
hash["red"]["dress"] = true
hash["red"]["truck"] = true  
hash["red"] => {"apple"=>true, "dress"=>true, "truck"=>true}

Counting and Totals

This class is also useful for building aggregate data about a set of similar objects, such as how many employees are in each department of each division of a company or total tax and sale amounts for a set of invoices by customer by year.

Counting example:

h = HashMore.new
employees = Employee.all
for e in employees 
  h[e.division][e.department]+=1
end

h['sales']['east coast] => 28
h['sales']['west coast] => 23

Totals example:

h = HashMore.new
invoices = Invoices.all

for i in invoices
  h[i.customer.name][i.sale_date.year]['tax'] += i.tax_amount
  h[i.customer.name][i.sale_date.year]['sale'] += i.sale_amount

end

h['Acme Corp']['2008']['tax'] => 12039.12
h['Acme Corp']['2009']['sale'] => 102649