Sha256: 98a56de9ae1bc024c8a3b2c72ab7c6722217db6fa59a0a528b8cf1ad4f2ec3ee

Contents?: true

Size: 866 Bytes

Versions: 28

Compression:

Stored size: 866 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2013
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>

# simple auto-vivifying hash
# TODO support nested hashes
def vivaHash default=[]
  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

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
muflax-0.5.1 lib/muflax/hash.rb
muflax-0.5.0 lib/muflax/hash.rb
muflax-0.4.2 lib/muflax/hash.rb
muflax-0.4.1 lib/muflax/hash.rb
muflax-0.4.0 lib/muflax/hash.rb
muflax-0.3.23 lib/muflax/hash.rb
muflax-0.3.22 lib/muflax/hash.rb
muflax-0.3.21 lib/muflax/hash.rb
muflax-0.3.20 lib/muflax/hash.rb
muflax-0.3.19 lib/muflax/hash.rb
muflax-0.3.18 lib/muflax/hash.rb
muflax-0.3.17 lib/muflax/hash.rb
muflax-0.3.16 lib/muflax/hash.rb
muflax-0.3.14 lib/muflax/hash.rb
muflax-0.3.13 lib/muflax/hash.rb
muflax-0.3.12 lib/muflax/hash.rb
muflax-0.3.11 lib/muflax/hash.rb
muflax-0.3.10 lib/muflax/hash.rb
muflax-0.3.9 lib/muflax/hash.rb
muflax-0.3.8 lib/muflax/hash.rb