Sha256: db0c52957a9598a50bb596d9240472d823768ccee8841a6372fd7c8dc49d44fb

Contents?: true

Size: 1.05 KB

Versions: 17

Compression:

Stored size: 1.05 KB

Contents

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(&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

17 entries across 17 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/hash/each.rb
facets-1.0.0 lib/facet/hash/each.rb
facets-1.0.3 packages/core/lib/facet/hash/each.rb
facets-1.3.0 lib/facets/core/hash/each.rb
facets-1.1.0 lib/facet/hash/each.rb
facets-1.2.0 lib/facets/core/hash/each.rb
facets-1.2.1 lib/facets/core/hash/each.rb
facets-1.3.3 lib/facets/core/hash/each.rb
facets-1.3.2 lib/facets/core/hash/each.rb
facets-1.3.1 lib/facets/core/hash/each.rb
facets-1.4.2 lib/facets/core/hash/each.rb
facets-1.4.1 lib/facets/core/hash/each.rb
facets-1.4.0 lib/facets/core/hash/each.rb
facets-1.4.3 lib/facets/core/hash/each.rb
facets-1.4.4 lib/facets/core/hash/each.rb
facets-1.4.5 lib/facets/core/hash/each.rb
facets-1.7.0 lib/facets/core/hash/each.rb