summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--NEWS2
-rw-r--r--array.c27
3 files changed, 24 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index dba1928bf5..5273505841 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sat Apr 3 06:56:11 2010 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * array.c (rb_ary_product): Accept a block [ruby-core:29045]
+
Sat Apr 3 04:46:00 2010 Tanaka Akira <akr@fsij.org>
* tool/file2lastrev.rb (VCS#relative_to): path and @srcdir may have
diff --git a/NEWS b/NEWS
index 38a57c4cf7..67bb1bb641 100644
--- a/NEWS
+++ b/NEWS
@@ -22,7 +22,7 @@ with all sufficient information, see the ChangeLog file.
* Array#sort_by!
* extended methods:
- * Array#{uniq,uniq!} can take a block.
+ * Array#{uniq,uniq!,product} can take a block.
* Dir
* new method:
diff --git a/array.c b/array.c
index 744d1e0491..64bfcfcf33 100644
--- a/array.c
+++ b/array.c
@@ -4052,11 +4052,15 @@ rb_ary_combination(VALUE ary, VALUE num)
/*
* call-seq:
- * ary.product(other_ary, ...)
+ * ary.product(other_ary, ...) -> array
+ * ary.product(other_ary, ...) { |p| block } -> ary
*
- * Returns an array of all combinations of elements from all arrays.
+ * Returns an array of all combinations of elements from all arrays,
* The length of the returned array is the product of the length
- * of ary and the argument arrays
+ * of ary and the argument arrays.
+ * If given a block, <i>product</i> will yield all combinations
+ * and return self instead.
+ *
*
* [1,2,3].product([4,5]) # => [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
* [1,2].product([1,2]) # => [[1,1],[1,2],[2,1],[2,2]]
@@ -4074,7 +4078,7 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
volatile VALUE t1 = tmpbuf(n, sizeof(int));
VALUE *arrays = (VALUE*)RSTRING_PTR(t0); /* The arrays we're computing the product of */
int *counters = (int*)RSTRING_PTR(t1); /* The current position in each one */
- VALUE result; /* The array we'll be returning */
+ VALUE result = Qnil; /* The array we'll be returning, when no block given */
long i,j;
long resultlen = 1;
@@ -4091,7 +4095,7 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
/* Compute the length of the result array; return [] if any is empty */
for (i = 0; i < n; i++) {
long k = RARRAY_LEN(arrays[i]), l = resultlen;
- if (k == 0) return rb_ary_new2(0);
+ if (k == 0) return rb_block_given_p() ? ary : rb_ary_new2(0);
resultlen *= k;
if (resultlen < k || resultlen < l || resultlen / k != l) {
rb_raise(rb_eRangeError, "too big to product");
@@ -4099,7 +4103,9 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
}
/* Otherwise, allocate and fill in an array of results */
- result = rb_ary_new2(resultlen);
+ if (!rb_block_given_p()) {
+ result = rb_ary_new2(resultlen);
+ }
for (i = 0; i < resultlen; i++) {
int m;
/* fill in one subarray */
@@ -4109,7 +4115,12 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
}
/* put it on the result array */
- rb_ary_push(result, subarray);
+ if(NIL_P(result)) {
+ rb_yield(subarray);
+ }
+ else {
+ rb_ary_push(result, subarray);
+ }
/*
* Increment the last counter. If it overflows, reset to 0
@@ -4126,7 +4137,7 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
tmpbuf_discard(t0);
tmpbuf_discard(t1);
- return result;
+ return NIL_P(result) ? ary : result;
}
/*