summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--array.c80
2 files changed, 61 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index d3ea6c3445..41b4ca129b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,13 @@
+Wed Apr 9 20:47:16 2008 Akinori MUSHA <knu@iDaemons.org>
+
+ * array.c (rb_ary_index, rb_ary_index): Array#index and #rindex
+ take a block instead of an argument; backported from 1.9.
+
Wed Apr 9 19:58:31 2008 Akinori MUSHA <knu@iDaemons.org>
* enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
ext/enumerator, common.mk (OBJS, enumerator.$(OBJEXT)): Make the
- enumerator module built-in,
+ enumerator module built-in.
* enumerator.c: New method: Enumerable::Enumerator#with_index.
diff --git a/array.c b/array.c
index 2d345dab5d..41b1224e4e 100644
--- a/array.c
+++ b/array.c
@@ -848,26 +848,41 @@ rb_ary_fetch(argc, argv, ary)
/*
* call-seq:
- * array.index(obj) -> int or nil
+ * array.index(obj) -> int or nil
+ * array.index {|item| block} -> int or nil
*
- * Returns the index of the first object in <i>self</i> such that is
- * <code>==</code> to <i>obj</i>. Returns <code>nil</code> if
- * no match is found.
+ * Returns the index of the first object in <i>self</i> such that is
+ * <code>==</code> to <i>obj</i>. If a block is given instead of an
+ * argument, returns first object for which <em>block</em> is true.
+ * Returns <code>nil</code> if no match is found.
*
* a = [ "a", "b", "c" ]
- * a.index("b") #=> 1
- * a.index("z") #=> nil
+ * a.index("b") #=> 1
+ * a.index("z") #=> nil
+ * a.index{|x|x=="b"} #=> 1
*/
static VALUE
-rb_ary_index(ary, val)
+rb_ary_index(argc, argv, ary)
+ int argc;
+ VALUE *argv;
VALUE ary;
- VALUE val;
{
+ VALUE val;
long i;
- for (i=0; i<RARRAY(ary)->len; i++) {
- if (rb_equal(RARRAY(ary)->ptr[i], val))
+ 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, "01", &val);
+ for (i=0; i<RARRAY_LEN(ary); i++) {
+ if (rb_equal(RARRAY_PTR(ary)[i], val))
return LONG2NUM(i);
}
return Qnil;
@@ -877,29 +892,44 @@ rb_ary_index(ary, val)
* call-seq:
* array.rindex(obj) -> int or nil
*
- * Returns the index of the last object in <i>array</i>
- * <code>==</code> to <i>obj</i>. Returns <code>nil</code> if
- * no match is found.
+ * Returns the index of the last object in <i>array</i>
+ * <code>==</code> to <i>obj</i>. If a block is given instead of an
+ * argument, returns first object for which <em>block</em> is
+ * true. Returns <code>nil</code> if no match is found.
*
* a = [ "a", "b", "b", "b", "c" ]
- * a.rindex("b") #=> 3
- * a.rindex("z") #=> nil
+ * a.rindex("b") #=> 3
+ * a.rindex("z") #=> nil
+ * a.rindex{|x|x=="b"} #=> 3
*/
static VALUE
-rb_ary_rindex(ary, val)
+rb_ary_rindex(argc, argv, ary)
+ int argc;
+ VALUE *argv;
VALUE ary;
- VALUE val;
{
- long i = RARRAY(ary)->len;
+ VALUE val;
+ long i = RARRAY_LEN(ary);
- while (i--) {
- if (i > RARRAY(ary)->len) {
- i = RARRAY(ary)->len;
- continue;
+ if (argc == 0) {
+ RETURN_ENUMERATOR(ary, 0, 0);
+ while (i--) {
+ if (RTEST(rb_yield(RARRAY_PTR(ary)[i])))
+ return LONG2NUM(i);
+ if (i > RARRAY_LEN(ary)) {
+ i = RARRAY_LEN(ary);
+ }
}
- if (rb_equal(RARRAY(ary)->ptr[i], val))
+ return Qnil;
+ }
+ rb_scan_args(argc, argv, "01", &val);
+ while (i--) {
+ if (rb_equal(RARRAY_PTR(ary)[i], val))
return LONG2NUM(i);
+ if (i > RARRAY_LEN(ary)) {
+ i = RARRAY_LEN(ary);
+ }
}
return Qnil;
}
@@ -3036,8 +3066,8 @@ Init_Array()
rb_define_method(rb_cArray, "length", rb_ary_length, 0);
rb_define_alias(rb_cArray, "size", "length");
rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0);
- rb_define_method(rb_cArray, "index", rb_ary_index, 1);
- rb_define_method(rb_cArray, "rindex", rb_ary_rindex, 1);
+ rb_define_method(rb_cArray, "index", rb_ary_index, -1);
+ rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1);
rb_define_method(rb_cArray, "indexes", rb_ary_indexes, -1);
rb_define_method(rb_cArray, "indices", rb_ary_indexes, -1);
rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);