Project

hashlib

0.0
No commit activity in last 3 years
No release in over 3 years
Hashlib extends the base Ruby Hash class with new methods that offer useful functionality for working with hashes, specifically addressing handling of deeply-nested Hashes for representing rich object structures.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

[UNMAINTAINED] Hashlib: Utility methods for Ruby Hashes

NOTE: This package is no longer being maintained.

Hashlib extends the base Ruby Hash class with new methods that offer useful functionality for working with hashes, specifically addressing handling of deeply-nested Hashes for representing rich object structures.

get

The get method is used to allow for retrieval of a deeply-nested value in a hash structure.

Given:

config = {
  :global => {
    :security => {
      :sslroot => '/etc/ssl'
    },
    :pidfile => '/var/run/example.pid'
  },
  :plugins => ['logger', 'cruncher']
}

The following statements are equivalent:

config.get('global.security.sslroot')

# returns the same thing as

config[:global][:security][:sslroot]

However, let's say you attempted to get config[:global][:adapters][:path]. You would get a nasty NilError because :adapters doesn't exist. However, if you used config.get('global.adapters.path'), the result would just be nil. get also takes a second argument that let's you specify the default value if the given path is not found. This lets you very easily work with rich nested hashes while also specifying sane defaults for missing values.

set

The set method is the opposite of get. It creates one or more intermediary hashes along a specified path and setting the value for the last component.

y = {}
y.set('this.is.a.number', 4)

# results in
# {"this"=>{
#    "is"=>{
#      "a"=>{
#        "number"=>4
# }}}}