Sha256: b14a638d99860f638585ecb2877217a7bddf3ef7e6cef2bd1fcdd17ac3fc02fb

Contents?: true

Size: 690 Bytes

Versions: 5

Compression:

Stored size: 690 Bytes

Contents

# source: https://github.com/rubyworks/facets
class Hash

  # Apply a block to hash, and recursively apply that block
  # to each sub-hash or +types+.
  #
  #   h = {:a=>1, :b=>{:b1=>1, :b2=>2}}
  #   g = h.recurse{|h| h.inject({}){|h,(k,v)| h[k.to_s] = v; h} }
  #   g  #=> {"a"=>1, "b"=>{"b1"=>1, "b2"=>2}}
  #
  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

  # In place form of #recurse.
  def recurse!(&block)
    replace(recurse(&block))
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
endeca_on_demand-1.3.4 lib/endeca_on_demand/core_ext/hash.rb
endeca_on_demand-1.3.3 lib/endeca_on_demand/core_ext/hash.rb
endeca_on_demand-1.3.2 lib/endeca_on_demand/core_ext/hash.rb
endeca_on_demand-1.3.1 lib/endeca_on_demand/core_ext/hash.rb
endeca_on_demand-1.3.0 lib/endeca_on_demand/core_ext/hash.rb