Project

hash-class

0.0
No commit activity in last 3 years
No release in over 3 years
Extends Ruby's Hash object with assign_if
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

> 10.0.0
> 3.0.0
 Project Readme

Hash-class will extend the Hash object with assign_if. This function reduces the verbosity when conditionaly assigning the value by key (within the basic Hash object).

Example:

# old
a = hash[:some_element] if hash.include?(:some_element)
# new
hash.assign_if(:some_element) {|e| a = e}

Logic

Block {|e| a = e} is yielded when key :some_element in hash is found. e contains the value of key :some_element. NOTE: When the key is not found, the block is not yielded!

Usage

Add gem to your Gemfile

gem 'hash-class', github: 'dblommesteijn/hash-class'

Use as standalone gem

gem install specific_install
gem specific_install https://github.com/dblommesteijn/hash-class

Code snippet

require 'hash-class'
a = 123
hash = {}
hash.assign_if(:some_element) {|e| a = e}

Run Tests

ruby -I test test/unit/test_hash.rb