Module: Gemmy::Patches::HashPatch::InstanceMethods::Recurse

Defined in:
lib/gemmy/patches/hash_patch.rb

Instance Method Summary collapse

Instance Method Details

#recurse(*types) {|h| ... } ⇒ Object

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

Yields:

  • (h)


150
151
152
153
154
155
156
157
158
159
160
161
162
# 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