Sha256: f561aeeef485a054eb821a74ea900194d0526272308adb8d1bc1deb322534440

Contents?: true

Size: 530 Bytes

Versions: 13

Compression:

Stored size: 530 Bytes

Contents

class Hash

  # Apply a block to hash, and recursively apply that block
  # to each subhash.
  #
  #   h = {:a=>1, :b=>{:b1=>1, :b2=>2}}
  #   h.recursively{|h| h.rekey(&:to_s) }
  #   => {"a"=>1, "b"=>{"b1"=>1, "b2"=>2}}
  #
  def recursively(&block)
    h = inject({}) do |hash, (key, value)|
      if value.is_a?(Hash)
        hash[key] = value.recursively(&block)
      else
        hash[key] = value
      end
      hash
    end
    yield h
  end

  #

  def recursively!(&block)
    replace(recursively(&block))
  end

end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
facets-2.8.2 lib/core/facets/hash/recursively.rb
facets-2.8.1 lib/core/facets/hash/recursively.rb
facets-2.8.0 lib/core/facets/hash/recursively.rb
facets-2.7.0 lib/core/facets/hash/recursively.rb
facets-2.6.0 lib/core/facets/hash/recursively.rb
facets-2.4.2 lib/core/facets/hash/recursively.rb
facets-2.4.3 lib/core/facets/hash/recursively.rb
facets-2.4.4 lib/core/facets/hash/recursively.rb
facets-2.5.0 lib/core/facets/hash/recursively.rb
facets-2.4.5 lib/core/facets/hash/recursively.rb
facets-2.5.1 lib/core/facets/hash/recursively.rb
facets-2.5.2 lib/core/facets/hash/recursively.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/recursively.rb