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
a48e0018
Commit
a48e0018
authored
Apr 21, 2014
by
Jun Hiroe
Committed by
Yukihiro "Matz" Matsumoto
Apr 23, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Array#insert
parent
951f027b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
+36
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+27
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+9
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
a48e0018
...
...
@@ -490,4 +490,31 @@ class Array
self
end
end
##
# call-seq:
# ary.insert(index, obj...) -> ary
#
# Inserts the given values before the element with the given +index+.
#
# Negative indices count backwards from the end of the array, where +-1+ is
# the last element.
#
# a = %w{ a b c d }
# a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
# a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
def
insert
(
idx
,
*
obj
)
idx
+=
self
.
size
+
1
if
idx
<
0
ary
=
[]
before_ary
=
self
[
0
,
idx
]
after_ary
=
self
[
idx
,
self
.
size
]
before_ary
.
each
{
|
val
|
ary
<<
val
}
unless
before_ary
==
nil
while
ary
.
size
<
idx
ary
<<
nil
end
obj
.
each
{
|
val
|
ary
<<
val
}
unless
obj
==
nil
after_ary
.
each
{
|
val
|
ary
<<
val
}
unless
after_ary
==
nil
self
.
replace
(
ary
)
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
a48e0018
...
...
@@ -197,3 +197,12 @@ assert("Array#reject!") do
assert_equal
[
1
,
2
,
3
],
a
.
reject!
{
|
val
|
val
>
3
}
assert_equal
[
1
,
2
,
3
],
a
end
assert
(
"Array#insert"
)
do
a
=
[
"a"
,
"b"
,
"c"
,
"d"
]
assert_equal
[
"a"
,
"b"
,
99
,
"c"
,
"d"
],
a
.
insert
(
2
,
99
)
assert_equal
[
"a"
,
"b"
,
99
,
"c"
,
1
,
2
,
3
,
"d"
],
a
.
insert
(
-
2
,
1
,
2
,
3
)
b
=
[
"a"
,
"b"
,
"c"
,
"d"
]
assert_equal
[
"a"
,
"b"
,
"c"
,
"d"
,
nil
,
nil
,
99
],
b
.
insert
(
6
,
99
)
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