lib/flapjack/utility.rb in flapjack-0.7.2 vs lib/flapjack/utility.rb in flapjack-0.7.3

- old
+ new

@@ -50,7 +50,23 @@ return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo} if obj.is_a? Hash return obj.inject([]){|memo,v | memo << symbolize(v); memo} if obj.is_a? Array return obj end + # The passed block will be provided each value from the args + # and must return array pairs [key, value] representing members of + # the hash this method returns. Keys should be unique -- if they're + # not, the earlier pair for that key will be overwritten. + def hashify(*args, &block) + key_value_pairs = args.map {|a| yield(a) } + + # if using Ruby 1.9, + # Hash[ key_value_pairs ] + # is all that's needed, but for Ruby 1.8 compatability, these must + # be flattened and the resulting array unpacked. flatten(1) only + # flattens the arrays constructed in the block, it won't mess up + # any values (or keys) that are themselves arrays/hashes. + Hash[ *( key_value_pairs.flatten(1) )] + end + end end