Module: Aquarium::Utils::HashUtils
- Includes:
- ArrayUtils
- Defined in:
- lib/aquarium/utils/hash_utils.rb
Instance Method Summary collapse
-
#make_hash(item_or_array_or_hash) ⇒ Object
Convert the input item or array into a hash with a nil value or the result of evaluating the optional input block, which takes a single argument for the item.
- #strip_nil_keys(hash) ⇒ Object
Methods included from ArrayUtils
#make_array, make_array, #strip_array_nils, strip_array_nils
Instance Method Details
#make_hash(item_or_array_or_hash) ⇒ Object
Convert the input item or array into a hash with a nil value or the result of evaluating the optional input block, which takes a single argument for the item. If the input is already a hash, it is returned unmodified.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/aquarium/utils/hash_utils.rb', line 11 def make_hash item_or_array_or_hash return {} if item_or_array_or_hash.nil? return strip_nil_keys(item_or_array_or_hash) if item_or_array_or_hash.kind_of?(Hash) hash = {} [item_or_array_or_hash].flatten.each do |element| unless element.nil? hash[element] = block_given? ? yield(element) : nil end end hash end |
#strip_nil_keys(hash) ⇒ Object
23 24 25 |
# File 'lib/aquarium/utils/hash_utils.rb', line 23 def strip_nil_keys hash hash.reject {|k,v| k.nil?} end |