Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
68695d17
Commit
68695d17
authored
Mar 27, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Enumerable#zip
parent
70718d59
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+30
-0
mrbgems/mruby-enum-ext/test/enum.rb
mrbgems/mruby-enum-ext/test/enum.rb
+9
-0
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
68695d17
...
@@ -615,4 +615,34 @@ module Enumerable
...
@@ -615,4 +615,34 @@ module Enumerable
end
end
nil
nil
end
end
##
# call-seq:
# enum.zip(arg, ...) -> an_array_of_array
#
# Takes one element from <i>enum</i> and merges corresponding
# elements from each <i>args</i>. This generates a sequence of
# <em>n</em>-element arrays, where <em>n</em> is one more than the
# count of arguments. The length of the resulting sequence will be
# <code>enum#size</code>. If the size of any argument is less than
# <code>enum#size</code>, <code>nil</code> values are supplied.
#
def
zip
(
*
arg
)
ary
=
[]
i
=
0
self
.
each
do
|
val
|
a
=
[]
a
.
push
(
val
)
idx
=
0
while
idx
<
arg
.
size
a2
=
arg
[
idx
].
to_a
a
.
push
(
a2
[
i
])
idx
+=
1
end
ary
.
push
(
a
)
i
+=
1
end
ary
end
end
end
mrbgems/mruby-enum-ext/test/enum.rb
View file @
68695d17
...
@@ -135,3 +135,12 @@ assert("Enumerable#find_index") do
...
@@ -135,3 +135,12 @@ assert("Enumerable#find_index") do
assert_equal
34
,
(
1
..
100
).
find_index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
assert_equal
34
,
(
1
..
100
).
find_index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
assert_equal
49
,(
1
..
100
).
find_index
(
50
)
assert_equal
49
,(
1
..
100
).
find_index
(
50
)
end
end
assert
(
"Enumerable#zip"
)
do
a
=
[
4
,
5
,
6
]
b
=
[
7
,
8
,
9
]
assert_equal
[[
4
,
7
],
[
5
,
8
],
[
6
,
9
]],
a
.
zip
(
b
)
assert_equal
[[
1
,
4
,
7
],
[
2
,
5
,
8
],
[
3
,
6
,
9
]],
[
1
,
2
,
3
].
zip
(
a
,
b
)
assert_equal
[[
1
,
4
,
7
],
[
2
,
5
,
8
]],
[
1
,
2
].
zip
(
a
,
b
)
assert_equal
[[
4
,
1
,
8
],
[
5
,
2
,
nil
],
[
6
,
nil
,
nil
]],
a
.
zip
([
1
,
2
],
[
8
])
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment