summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorksss <[email protected]>2014-03-17 22:31:40 +0900
committerksss <[email protected]>2014-03-17 22:31:40 +0900
commit9abe413530431660b274acb7273ef1f1f23771c7 (patch)
tree63ab946bc58d97a7d1f695d002534f5be287b303
parent2f8dbd8e5de04694ee2c2f7ad50418b184d2a08f (diff)
downloadmruby-9abe413530431660b274acb7273ef1f1f23771c7.tar.gz
mruby-9abe413530431660b274acb7273ef1f1f23771c7.zip
Enumrable#each_with_index return Enumerator if non block given
-rw-r--r--mrbgems/mruby-enumerator/test/enumerator.rb4
-rw-r--r--mrblib/enum.rb2
2 files changed, 6 insertions, 0 deletions
diff --git a/mrbgems/mruby-enumerator/test/enumerator.rb b/mrbgems/mruby-enumerator/test/enumerator.rb
index 4ab857ae8..45d86685e 100644
--- a/mrbgems/mruby-enumerator/test/enumerator.rb
+++ b/mrbgems/mruby-enumerator/test/enumerator.rb
@@ -438,6 +438,10 @@ assert 'Integral#times' do
assert_equal [0,1,2], c
end
+assert 'Enumerable#each_with_index' do
+ assert_equal [['a',0],['b',1],['c',2]], ['a','b','c'].each_with_index.to_a
+end
+
assert 'Enumerable#map' do
a = [1,2,3]
b = a.map
diff --git a/mrblib/enum.rb b/mrblib/enum.rb
index 5a33ed3c5..04a27294c 100644
--- a/mrblib/enum.rb
+++ b/mrblib/enum.rb
@@ -108,6 +108,8 @@ module Enumerable
#
# ISO 15.3.2.2.5
def each_with_index(&block)
+ return to_enum :each_with_index unless block_given?
+
i = 0
self.each{|*val|
block.call(val.__svalue, i)