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
e8cb971b
Commit
e8cb971b
authored
Mar 28, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1969 from suzukaze/add-array.fetch
Add Array#fetch
parents
56251042
de573e57
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+41
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+11
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
e8cb971b
...
@@ -211,4 +211,45 @@ class Array
...
@@ -211,4 +211,45 @@ class Array
end
end
self
self
end
end
NONE
=
Object
.
new
##
# call-seq:
# ary.fetch(index) -> obj
# ary.fetch(index, default) -> obj
# ary.fetch(index) { |index| block } -> obj
#
# Tries to return the element at position +index+, but throws an IndexError
# exception if the referenced +index+ lies outside of the array bounds. This
# error can be prevented by supplying a second argument, which will act as a
# +default+ value.
#
# Alternatively, if a block is given it will only be executed when an
# invalid +index+ is referenced. Negative values of +index+ count from the
# end of the array.
#
# a = [ 11, 22, 33, 44 ]
# a.fetch(1) #=> 22
# a.fetch(-1) #=> 44
# a.fetch(4, 'cat') #=> "cat"
# a.fetch(100) { |i| puts "#{i} is out of bounds" }
# #=> "100 is out of bounds"
#
def
fetch
(
n
=
nil
,
ifnone
=
NONE
,
&
block
)
warn
"block supersedes default value argument"
if
n
!=
nil
&&
ifnone
!=
NONE
&&
block
idx
=
n
if
idx
<
0
idx
+=
size
end
if
idx
<
0
||
size
<=
idx
return
block
.
call
(
n
)
if
block
if
ifnone
==
NONE
raise
IndexError
,
"index
#{
n
}
outside of array bounds:
#{
-
size
}
...
#{
size
}
"
end
return
ifnone
end
self
[
idx
]
end
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
e8cb971b
...
@@ -107,3 +107,14 @@ assert("Array#compact!") do
...
@@ -107,3 +107,14 @@ assert("Array#compact!") do
a
.
compact!
a
.
compact!
a
==
[
1
,
"2"
,
:t
,
false
]
a
==
[
1
,
"2"
,
:t
,
false
]
end
end
assert
(
"Array#fetch"
)
do
a
=
[
11
,
22
,
33
,
44
]
assert_equal
22
,
a
.
fetch
(
1
)
assert_equal
44
,
a
.
fetch
(
-
1
)
assert_equal
'cat'
,
a
.
fetch
(
4
,
'cat'
)
ret
=
0
a
.
fetch
(
100
)
{
|
i
|
ret
=
i
}
assert_equal
100
,
ret
assert_raise
(
IndexError
)
{
a
.
fetch
(
100
)
}
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