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
fafe86d3
Commit
fafe86d3
authored
Oct 01, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Array#index to take block; fix #2968 close #2970
parent
d12926da
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
0 deletions
+34
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+28
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+5
-0
src/array.c
src/array.c
+1
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
fafe86d3
...
...
@@ -681,4 +681,32 @@ class Array
return
nil
if
self
.
size
==
result
.
size
self
.
replace
(
result
)
end
##
# call-seq:
# ary.index(val) -> int or nil
# ary.index {|item| block } -> int or nil
#
# Returns the _index_ of the first object in +ary+ such that the object is
# <code>==</code> to +obj+.
#
# If a block is given instead of an argument, returns the _index_ of the
# first object for which the block returns +true+. Returns +nil+ if no
# match is found.
#
# ISO 15.2.12.5.14
def
index
(
val
=
NONE
,
&
block
)
return
to_enum
(
:find_index
,
val
)
if
!
block
&&
val
==
NONE
if
block
idx
=
0
self
.
each
do
|*
e
|
return
idx
if
block
.
call
(
*
e
)
idx
+=
1
end
else
return
self
.
__ary_index
(
val
)
end
nil
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
fafe86d3
...
...
@@ -293,3 +293,8 @@ assert('Array#to_h') do
assert_raise
(
TypeError
)
{
[
1
].
to_h
}
assert_raise
(
ArgumentError
)
{
[[
1
]].
to_h
}
end
assert
(
"Array#index (block)"
)
do
assert_nil
(
1
..
10
).
to_a
.
index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
assert_equal
34
,
(
1
..
100
).
to_a
.
index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
end
src/array.c
View file @
fafe86d3
...
...
@@ -1101,4 +1101,5 @@ mrb_init_array(mrb_state *mrb)
mrb_define_method
(
mrb
,
a
,
"__ary_eq"
,
mrb_ary_eq
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"__ary_cmp"
,
mrb_ary_cmp
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"__ary_index"
,
mrb_ary_index_m
,
MRB_ARGS_REQ
(
1
));
/* kept for mruby-array-ext */
}
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