[macruby-changes] [5017] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Dec 13 00:25:17 PST 2010


Revision: 5017
          http://trac.macosforge.org/projects/ruby/changeset/5017
Author:   watson1978 at gmail.com
Date:     2010-12-13 00:25:10 -0800 (Mon, 13 Dec 2010)
Log Message:
-----------
When given a block, Array#product will yield a product element  and return a self.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

ary    = [1, 2]
result = []
ret1 = ary.product([3,4,5],[6,8]) {|a| result << a}
assert_equal(ary, ret1)

ret2 = ary.product([3,4,5],[6,8])
assert_equal(result, ret2)

puts :ok
}}}

Modified Paths:
--------------
    MacRuby/trunk/array.c
    MacRuby/trunk/spec/frozen/tags/macruby/core/array/product_tags.txt

Modified: MacRuby/trunk/array.c
===================================================================
--- MacRuby/trunk/array.c	2010-12-12 07:47:06 UTC (rev 5016)
+++ MacRuby/trunk/array.c	2010-12-13 08:25:10 UTC (rev 5017)
@@ -3417,7 +3417,7 @@
     int n = argc+1;    /* How many arrays we're operating on */
     VALUE *arrays = (VALUE *)alloca(n * sizeof(VALUE));; /* The arrays we're computing the product of */
     int *counters = (int *)alloca(n * sizeof(int)); /* The current position in each one */
-    VALUE result;      /* The array we'll be returning */
+    VALUE result = Qnil; /* The array we'll be returning */
     long i,j;
     long resultlen = 1;
 
@@ -3438,8 +3438,10 @@
 	}
     }
 
-    /* Otherwise, allocate and fill in an array of results */
-    result = rb_ary_new2(resultlen);
+    if (!rb_block_given_p()) {
+	/* Otherwise, allocate and fill in an array of results */
+	result = rb_ary_new2(resultlen);
+    }
     for (i = 0; i < resultlen; i++) {
 	int m;
 	/* fill in one subarray */
@@ -3448,8 +3450,13 @@
 	    rb_ary_push(subarray, rb_ary_entry(arrays[j], counters[j]));
 	}
 
-	/* put it on the result array */
-	rb_ary_push(result, subarray);
+	if (NIL_P(result)) {
+	    rb_yield(subarray);
+	}
+	else {
+	    /* put it on the result array */
+	    rb_ary_push(result, subarray);
+	}
 
 	/*
 	 * Increment the last counter.  If it overflows, reset to 0
@@ -3464,7 +3471,7 @@
 	}
     }
 
-    return result;
+    return NIL_P(result) ? ary : result;
 }
 
 /*

Modified: MacRuby/trunk/spec/frozen/tags/macruby/core/array/product_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/array/product_tags.txt	2010-12-12 07:47:06 UTC (rev 5016)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/array/product_tags.txt	2010-12-13 08:25:10 UTC (rev 5017)
@@ -1,2 +1 @@
-fails:Array#product when given a block yields all combinations in turn
 fails:Array#product when given an empty block returns self
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101213/88839003/attachment.html>


More information about the macruby-changes mailing list