module Aquarium::Utils::HashUtils

Public Instance Methods

make_hash(item_or_array_or_hash) { |element| ... } click to toggle source

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.

   # File lib/aquarium/utils/hash_utils.rb
11 def make_hash item_or_array_or_hash
12   return {} if item_or_array_or_hash.nil? 
13   return strip_nil_keys(item_or_array_or_hash) if item_or_array_or_hash.kind_of?(Hash)
14   hash = {}
15   [item_or_array_or_hash].flatten.each do |element| 
16     unless element.nil?
17       hash[element] = block_given? ? yield(element) : nil
18     end
19   end
20   hash
21 end
strip_nil_keys(hash) click to toggle source
   # File lib/aquarium/utils/hash_utils.rb
23 def strip_nil_keys hash
24   hash.reject {|k,v| k.nil?}
25 end