Giter Club home page Giter Club logo

nhash's Introduction

NHash

This gem solves the common need for an n-dimensional hash that is pre-initialized.

In addition, it offers a novel means of slicing through N-dimensional hashes.

Installation

Add this line to your application's Gemfile:

gem 'NHash'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install NHash

Usage

Initialize NHash with a number of dimensions, 3 by default. Values may then be set/retrieved using a list of keys, like so:

> n = NHash.new(3)
> n[1, 2, 3] = :foo
> n[:a, :b, :c] = :bar
> n.to_h
{1 => { 2 => { 3 => :foo}}, a: { b: { c: :bar }}}

Keys may be of any type, but are converted to symbols internally when possible for indifferent access

> n['a', :b, 'd'] = :baz
> n.to_h
{1 => { 2 => { 3 => :foo}}, a: { b: { c: :bar, d: :baz }}}

pre-initialization

It is not uncommon to see this pattern for a hash of hashes:

newhash = {}
things.each do |thing|
    newhash[thing.name] = {}
    thing.otherthing.each do |otherthing|
        newhash[thing.name][otherthing.name] = otherthing.result
    end
end

NHash allows:

newhash = NHash.new(2)
things.each do |thing|
    thing.otherthing.each do |otherthing|
        newhash[thing.name, otherthing.name] = otherthing.result
    end
end

slicing

Let's say we have a list of airport flights and their count of passengers

flight = NHash.new
flight[:ord, :lax] = 200
flight[:ord, :jfk] = 150
flight[:atl, :jfk] = 175

Find all flights from :ord like so:

> flight[:ord, nil]
{lax: 200, jfk: 150}

or to JFK as:

> flight[nil, :jfk]
{ord: 150, atl: 175}

too few parameters assumes the remainder are nil

> flight[:atl]
{jfk: 175}
> flight[:lax]
{}

slicing may only be used for reading, not assignment. In effect, any given parameter removes that level from the heirarchy

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also 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, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stephancom/NHash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the NHash project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

nhash's People

Contributors

stephancom 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.