8000 Array#index more logical and add a rdoc extension to the README files by robin850 · Pull Request #128 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Restore the previous version of the Array#index method and add a Arra…
…y#indexes method (with the index_all alias)
  • Loading branch information
robin850 committed Jun 10, 2012
commit 9a4dc029fa682f7857f336dc7f3ee1125bb59b5f
42 changes: 33 additions & 9 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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.

RETURN_ENUMERATOR(ary, 0, 0);
Copy link
Member

Choose a reason for hiding this comment

The 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 (\t) from the indentation of this line (and further lines). See https://bugs.ruby-lang.org/projects/ruby/wiki/DeveloperHowto#coding-style to format your patch correctly.

array_lenth = 0;
Expand All @@ -1246,16 +1277,7 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary)

return Qnil;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function shouldn't return twice.

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no return outside this if (argc == 0) { block, a "control reaches end of non-void function" error will be generated.

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
Expand Down Expand Up @@ -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);
Expand Down
0