Giter Site home page Giter Site logo

hashlib's Introduction

Hashlib: Utility methods for Ruby Hashes

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
# }}}}

hashlib's People

Contributors

ghetzel avatar

Watchers

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