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
8cdac822
Commit
8cdac822
authored
Mar 29, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1978 from suzukaze/add-array.fill
Add Array#fill
parents
a88403cc
aa4a18e8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
0 deletions
+110
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+97
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+13
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
8cdac822
...
...
@@ -254,4 +254,101 @@ class Array
end
self
[
idx
]
end
##
# call-seq:
# ary.fill(obj) -> ary
# ary.fill(obj, start [, length]) -> ary
# ary.fill(obj, range ) -> ary
# ary.fill { |index| block } -> ary
# ary.fill(start [, length] ) { |index| block } -> ary
# ary.fill(range) { |index| block } -> ary
#
# The first three forms set the selected elements of +self+ (which
# may be the entire array) to +obj+.
#
# A +start+ of +nil+ is equivalent to zero.
#
# A +length+ of +nil+ is equivalent to the length of the array.
#
# The last three forms fill the array with the value of the given block,
# which is passed the absolute index of each element to be filled.
#
# Negative values of +start+ count from the end of the array, where +-1+ is
# the last element.
#
# a = [ "a", "b", "c", "d" ]
# a.fill("x") #=> ["x", "x", "x", "x"]
# a.fill("w", -1) #=> ["x", "x", "x", "w"]
# a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
# a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
# a.fill { |i| i*i } #=> [0, 1, 4, 9]
# a.fill(-2) { |i| i*i*i } #=> [0, 1, 8, 27]
# a.fill(1, 2) { |i| i+1 } #=> [0, 2, 3, 27]
# a.fill(0..1) { |i| i+1 } #=> [1, 2, 3, 27]
#
def
fill
(
arg0
=
nil
,
arg1
=
nil
,
arg2
=
nil
,
&
block
)
if
arg0
==
nil
&&
arg1
==
nil
&&
arg2
==
nil
&&
!
block
raise
ArgumentError
,
"wrong number of arguments (0 for 1..3)"
end
beg
=
len
=
0
ary
=
[]
if
block
if
arg0
==
nil
&&
arg1
==
nil
&&
arg2
==
nil
# ary.fill { |index| block } -> ary
beg
=
0
len
=
self
.
size
elsif
arg0
!=
nil
&&
arg0
.
respond_to?
(
:begin
)
&&
arg0
.
respond_to?
(
:end
)
# ary.fill(range) { |index| block } -> ary
beg
=
arg0
.
begin
beg
+=
self
.
size
if
beg
<
0
len
=
arg0
.
end
-
beg
+
1
elsif
arg0
!=
nil
# ary.fill(start [, length] ) { |index| block } -> ary
beg
=
arg0
beg
+=
self
.
size
if
beg
<
0
if
arg1
==
nil
len
=
self
.
size
else
len
=
arg0
+
arg1
end
end
else
if
arg0
!=
nil
&&
arg1
==
nil
&&
arg2
==
nil
# ary.fill(obj) -> ary
beg
=
0
len
=
self
.
size
elsif
arg0
!=
nil
&&
arg1
!=
nil
&&
arg1
.
respond_to?
(
:begin
)
&&
arg1
.
respond_to?
(
:end
)
# ary.fill(obj, range ) -> ary
len
=
self
.
size
beg
=
arg1
.
begin
len
=
arg1
.
end
-
beg
+
1
elsif
arg0
!=
nil
&&
arg1
!=
nil
# ary.fill(obj, start [, length]) -> ary
beg
=
arg1
beg
+=
self
.
size
if
beg
<
0
if
arg2
==
nil
len
=
self
.
size
else
len
=
arg1
+
arg2
end
end
end
i
=
beg
if
block
while
i
<
len
self
[
i
]
=
block
.
call
(
i
)
i
+=
1
end
else
while
i
<
len
self
[
i
]
=
arg0
i
+=
1
end
end
self
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
8cdac822
...
...
@@ -118,3 +118,16 @@ assert("Array#fetch") do
assert_equal
100
,
ret
assert_raise
(
IndexError
)
{
a
.
fetch
(
100
)
}
end
assert
(
"Array#fill"
)
do
a
=
[
"a"
,
"b"
,
"c"
,
"d"
]
assert_equal
[
"x"
,
"x"
,
"x"
,
"x"
],
a
.
fill
(
"x"
)
assert_equal
[
"x"
,
"x"
,
"x"
,
"w"
],
a
.
fill
(
"w"
,
-
1
)
assert_equal
[
"x"
,
"x"
,
"z"
,
"z"
],
a
.
fill
(
"z"
,
2
,
2
)
assert_equal
[
"y"
,
"y"
,
"z"
,
"z"
],
a
.
fill
(
"y"
,
0
..
1
)
assert_equal
[
0
,
1
,
4
,
9
],
a
.
fill
{
|
i
|
i
*
i
}
assert_equal
[
0
,
1
,
8
,
27
],
a
.
fill
(
-
2
)
{
|
i
|
i
*
i
*
i
}
assert_equal
[
0
,
2
,
3
,
27
],
a
.
fill
(
1
,
2
)
{
|
i
|
i
+
1
}
assert_equal
[
1
,
2
,
3
,
27
],
a
.
fill
(
0
..
1
)
{
|
i
|
i
+
1
}
assert_raise
(
ArgumentError
)
{
a
.
fill
}
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