Commit 76b2ba88 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1916 from ksss/hash-enumerator

Hash#each_{key,value} support return Enumerator if non block given
parents 539d9cb8 5386cd9b
......@@ -486,6 +486,14 @@ assert 'Hash#each' do
assert_equal [[:a,1], [:b,2]], c.sort
end
assert 'Hash#each_key' do
assert_equal [:a,:b], {a:1,b:2}.each_key.to_a.sort
end
assert 'Hash#each_value' do
assert_equal [1,2], {a:1,b:2}.each_value.to_a.sort
end
assert 'Range#each' do
a = (1..5)
b = a.each
......
......@@ -69,6 +69,8 @@ class Hash
#
# ISO 15.2.13.4.10
def each_key(&block)
return to_enum :each_key unless block_given?
self.keys.each{|k| block.call(k)}
self
end
......@@ -93,6 +95,8 @@ class Hash
#
# ISO 15.2.13.4.11
def each_value(&block)
return to_enum :each_value unless block_given?
self.keys.each{|k| block.call(self[k])}
self
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment