module Gemmy::Patches::HashPatch::InstanceMethods::Recurse

Public Instance Methods

recurse(*types) { |h| ... } click to toggle source

facets h = {:a=>1, :b=>{:b1=>1, :b2=>2}} g = h.recurse{|h| h.inject({}){|h,(k,v)| h = v; h} } g #=> {“a”=>1, “b”=>{“b1”=>1, “b2”=>2}}

# File lib/gemmy/patches/hash_patch.rb, line 150
def recurse(*types, &block)
  types = [self.class] if types.empty?
  h = inject({}) do |hash, (key, value)|
    case value
    when *types
      hash[key] = value.recurse(*types, &block)
    else
      hash[key] = value
    end
    hash
  end
  yield h
end