Skip to content

Commit

Permalink
Hash#each_{key,value} support return Enumerator
Browse files Browse the repository at this point in the history
if non block given
  • Loading branch information
ksss committed Mar 22, 2014
1 parent 7182ef6 commit 5386cd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mrbgems/mruby-enumerator/test/enumerator.rb
Expand Up @@ -486,6 +486,14 @@ def (o = Object.new).each
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
Expand Down
4 changes: 4 additions & 0 deletions mrblib/hash.rb
Expand Up @@ -69,6 +69,8 @@ def each(&block)
#
# 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
Expand All @@ -93,6 +95,8 @@ def each_key(&block)
#
# 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
Expand Down

0 comments on commit 5386cd9

Please sign in to comment.