Created
June 29, 2011 07:43
-
-
Save brixen/1053346 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def m | |
yield :a | |
end | |
def n | |
yield :a, 1 | |
end | |
m { |x| p x } | |
n { |x| p x } | |
m { |*x| p x } | |
n { |*x| p x } | |
p [:a, :b].to_enum.to_a | |
p [:a, :b].each_with_index.to_a | |
p [:a, :b].each.to_a | |
[:a, :b].each_with_index { |x| p x } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sasha:rubinius2.0 brian$ ruby -v enum.rb | |
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[:a, :b] | |
[[:a, 0], [:b, 1]] | |
[:a, :b] | |
:a | |
:b | |
sasha:rubinius2.0 brian$ rbx -Xint -X19 -v enum.rb | |
rubinius 2.0.0dev (1.9.2 d027e8ae yyyy-mm-dd) [x86_64-apple-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[:a, :b] | |
[:a, :b] | |
[:a, :b] | |
:a | |
:b | |
sasha:rubinius2.0 brian$ rbx -Xint -X19 -v enum.rb | |
rubinius 2.0.0dev (1.9.2 d027e8ae yyyy-mm-dd) [x86_64-apple-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[[:a], [:b]] | |
[[:a, 0], [:b, 1]] | |
[[:a], [:b]] | |
:a | |
:b | |
sasha:rubinius2.0 brian$ git diff | |
diff --git a/kernel/common/enumerable.rb b/kernel/common/enumerable.rb | |
index eb458e8..9f92734 100644 | |
--- a/kernel/common/enumerable.rb | |
+++ b/kernel/common/enumerable.rb | |
@@ -881,7 +881,7 @@ module Enumerable | |
def to_a(*arg) | |
ary = [] | |
- each(*arg) { |o| ary << o } | |
+ each(*arg) { |*o| ary << o } | |
ary | |
end | |
sasha:rubinius2.0 brian$ rbx -Xint -X19 -v enum.rb | |
rubinius 2.0.0dev (1.9.2 d027e8ae yyyy-mm-dd) [x86_64-apple-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[:a, :b] | |
[[:a, 0], [:b, 1]] | |
[:a, :b] | |
[:a, 0] | |
[:b, 1] | |
sasha:rubinius2.0 brian$ git diff | |
diff --git a/kernel/common/enumerable.rb b/kernel/common/enumerable.rb | |
index eb458e8..3ba9a8c 100644 | |
--- a/kernel/common/enumerable.rb | |
+++ b/kernel/common/enumerable.rb | |
@@ -418,7 +418,7 @@ module Enumerable | |
idx = 0 | |
each do |o| | |
- yield o, idx | |
+ yield [o, idx] | |
idx += 1 | |
end | |
sasha:rubinius2.0 brian$ rbx -Xint -X19 -v enum.rb | |
rubinius 2.0.0dev (1.9.2 d027e8ae yyyy-mm-dd) [x86_64-apple-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[:a, :b] | |
[[:a, 0], [:b, 1]] | |
[:a, :b] | |
:a | |
:b | |
sasha:rubinius2.0 brian$ ruby -v enum.rb | |
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0] | |
:a | |
:a | |
[:a] | |
[:a, 1] | |
[:a, :b] | |
[[:a, 0], [:b, 1]] | |
[:a, :b] | |
:a | |
:b | |
sasha:rubinius2.0 brian$ git diff | |
diff --git a/kernel/common/enumerable.rb b/kernel/common/enumerable.rb | |
index eb458e8..513159a 100644 | |
--- a/kernel/common/enumerable.rb | |
+++ b/kernel/common/enumerable.rb | |
@@ -881,7 +881,14 @@ module Enumerable | |
def to_a(*arg) | |
ary = [] | |
- each(*arg) { |o| ary << o } | |
+ each(*arg) do |a, *b| | |
+ if b.empty? | |
+ ary << a | |
+ else | |
+ b.unshift a | |
+ ary << b | |
+ end | |
+ end | |
ary | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment