-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Array#index more logical and add a rdoc extension to the README files #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4b23810
2ad2ffa
3c2ae5a
9a4dc02
dcc4939
0a272cd
3b44e74
68e99d9
69a6728
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…y#indexes method (with the index_all alias)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1223,6 +1223,37 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary) | |
VALUE val; | ||
long i; | ||
|
||
if (argc == 0) { | ||
RETURN_ENUMERATOR(ary, 0, 0); | ||
for (i=0; i<RARRAY_LEN(ary); i++) { | ||
if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) { | ||
return LONG2NUM(i); | ||
} | ||
} | ||
return Qnil; | ||
} | ||
rb_scan_args(argc, argv, "1", &val); | ||
if (rb_block_given_p()) | ||
rb_warn("given block not used"); | ||
for (i=0; i<RARRAY_LEN(ary); i++) { | ||
if (rb_equal(RARRAY_PTR(ary)[i], val)) | ||
return LONG2NUM(i); | ||
} | ||
return Qnil; | ||
} | ||
|
||
|
||
/* | ||
* | ||
* | ||
*/ | ||
static VALUE | ||
rb_ary_indexes(int argc, VALUE *argv, VALUE ary) | ||
{ | ||
|
||
VALUE val; | ||
long i; | ||
|
||
if (argc == 0) { | ||
RETURN_ENUMERATOR(ary, 0, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation for this method doesn't describe this enumerator. You seem to have removed the tab ( |
||
array_lenth = 0; | ||
|
@@ -1246,16 +1277,7 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary) | |
|
||
return Qnil; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function shouldn't return twice. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there is no return outside this This will crash ruby. You should handle argc > 0 here. |
||
rb_scan_args(argc, argv, "1", &val); | ||
if (rb_block_given_p()) | ||
rb_warn("given block not used"); | ||
for (i=0; i<RARRAY_LEN(ary); i++) { | ||
if (rb_equal(RARRAY_PTR(ary)[i], val)) | ||
return LONG2NUM(i); | ||
} | ||
return Qnil; | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* ary.rindex(obj) -> int or nil | ||
|
@@ -5090,6 +5112,8 @@ Init_Array(void) | |
rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0); | ||
rb_define_method(rb_cArray, "find_index", rb_ary_index, -1); | ||
rb_define_method(rb_cArray, "index", rb_ary_index, -1); | ||
rb_define_method(rb_cArray, "indexes", rb_ary_indexes, -1); | ||
rb_define_method(rb_cArray, "index_all", rb_ary_indexes, -1); | ||
rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1); | ||
rb_define_method(rb_cArray, "join", rb_ary_join_m, -1); | ||
rb_define_method(rb_cArray, "reverse", rb_ary_reverse_m, 0); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no implementation for
argc > 0
, so this implementation does not match the documentation.