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
e63c1e1d
Commit
e63c1e1d
authored
Jan 10, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1655 from pbosetti/master
Added rewrite of Array#[] to mruby-array-ext gem
parents
3fff0e20
921e6e24
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+32
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+7
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
e63c1e1d
...
...
@@ -201,4 +201,36 @@ class Array
self
.
replace
(
result
)
end
end
##
# call-seq:
# ary[rng] -> ary slice
#
# Remeturns a slice of +ary+ according to the Range instance +rng+.
#
# a = [ "a", "b", "c", "d", "e" ]
# a[1] => "b"
# a[1,2] => ["b", "c"]
# a[1..-2] => ["b", "c", "d"]
#
def
[]
(
idx
,
len
=
nil
)
case
idx
when
Range
if
idx
.
last
<
0
then
len
=
self
.
length
-
idx
.
first
+
idx
.
last
+
1
else
len
=
idx
.
last
-
idx
.
first
+
1
end
return
self
.
slice
(
idx
.
first
,
len
)
when
Numeric
if
len
then
return
self
.
slice
(
idx
.
to_i
,
len
.
to_i
)
else
return
self
.
slice
(
idx
.
to_i
)
end
else
self
.
slice
(
idx
)
end
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
e63c1e1d
...
...
@@ -107,3 +107,10 @@ assert("Array#compact!") do
a
.
compact!
a
==
[
1
,
"2"
,
:t
,
false
]
end
assert
(
"Array#[]"
)
do
a
=
[
"a"
,
"b"
,
"c"
,
"d"
,
"e"
]
a
[
1.1
]
==
"b"
and
a
[
1
,
2
]
==
[
"b"
,
"c"
]
and
a
[
1
..-
2
]
==
[
"b"
,
"c"
,
"d"
]
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