lib/muflax/hash.rb in muflax-0.2.6 vs lib/muflax/hash.rb in muflax-0.3.0

- old
+ new

@@ -4,11 +4,31 @@ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html> # simple auto-vivifying hash # TODO support nested hashes def vivaHash default=[] - hash = Hash.new do |h, k| - h[k] = default.duplicable? ? default.dup : default - end + hash = if default.duplicable? ; Hash.new{|h, k| h[k] = default.dup} + else ; Hash.new{|h, k| h[k] = default} + end hash +end + +# missing methods +class Hash + def transform_keys + return enum_for(:transform_keys) { size } unless block_given? + result = {} + each_key do |key| + result[yield(key)] = self[key] + end + result + end unless method_defined? :transform_keys + + def transform_keys! + return enum_for(:transform_keys!) { size } unless block_given? + keys.each do |key| + self[yield(key)] = delete(key) + end + self + end unless method_defined? :transform_keys! end