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!
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
require 'hash-class'
a = 123
hash = {}
hash.assign_if(:some_element) {|e| a = e}
ruby -I test test/unit/test_hash.rb