Sha256: c49f0100419339af7dd1674a8bf14f6dad31066f608084236acd1c8521b99742

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

#--
# Special thanks to Daniel Schierbeck.
#++

# TODO Deprecate Hash#each
warn "Hash#each is being deprecated"


class Hash

  # A "smarter" hash#each which iterates through each
  # _value_ if only one block parameter is given.
  #
  #   {:a=>"a", 2=>"b", "x"=>"c"}.each{ |v| puts v }
  #
  # _produces_
  #
  #   a
  #   b
  #   c
  #
  # WARNING! Use with caution. Methods from other libraries 
  # may depend on the old behavior, expecting a two element 
  # array to be passed into a single block argument.

  def each(&block)
     if block.arity < 2
       each_value(&block)
     else
       each_pair(&block)
     end
   end 

# def each(&yld)
#   case yld.arity
#   when 0
#   when 1
#     each_value{|v| yield(v)}
#   else
#     each_pair{|k,v| yld.call(k,v)}
#   end
#   self
# end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCHash < Test::Unit::TestCase

    def test_each_1
      h = { :a=>1, :b=>2, :c=>3 }
      a = []
      h.each { |v| a << v }
      assert_equal( [1,2,3], a.sort )
   end

   def test_each_2
      h = { 'a'=>1, 'b'=>2, 'c'=>3 }
      a = []
      h.each { |k,v| a << [k,v] }
      assert_equal( [['a',1],['b',2],['c',3]], a.sort )
    end

  end

=end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-1.7.30 lib/facets/core/hash/each.rb
facets-1.8.0 work/pore/hash/each.rb
facets-1.8.20 work/pore/hash/each.rb
facets-1.8.49 work/pore/hash/each.rb
facets-1.8.8 work/pore/hash/each.rb