diff --git a/ChangeLog b/ChangeLog index aa7cc8ddb8..a534f741be 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Thu Oct 18 17:08:57 2007 Yukihiro Matsumoto + + * enum.c (enum_find_index): update RDoc. a patch from David Flanagan + in [ruby-core:12710]. + + * enum.c (enum_take, enum_drop): ditto. + + * enum.c (enum_cycle): should not cause infinite loop for empty + arrays. [ruby-core:12710] + Thu Oct 18 16:39:34 2007 Yukihiro Matsumoto * lib/rexml/source.rb (REXML::SourceFactory::SourceFactory): more @@ -143,7 +153,7 @@ Tue Oct 16 13:25:46 2007 Nobuyoshi Nakada Tue Oct 16 01:31:23 2007 Yukihiro Matsumoto * enum.c (enum_inject): RDoc update. a patch from David Flanagan - in [ruby-core:12679] + in [ruby-core:12710]. Tue Oct 16 01:25:40 2007 Yukihiro Matsumoto diff --git a/enum.c b/enum.c index 12e488eac6..0a64b5740d 100644 --- a/enum.c +++ b/enum.c @@ -178,14 +178,14 @@ find_index_i(VALUE i, VALUE *memo) /* * call-seq: - * enum.find_index(ifnone = nil) {| obj | block } => int + * enum.find_index() {| obj | block } => int * * Passes each entry in enum to block. Returns the * index for the first for which block is not false. * If no object matches, returns nil * * (1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> nil - * (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> 35 + * (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> 34 * */ @@ -1420,11 +1420,12 @@ take_iter_i(VALUE i, VALUE *arg) * enum.take(n) => array * enum.take {|arr| block } => array * - * Without a block, returns first n elements from enum - * With a block, takes elements during block evaluation gives - * true. + * Without a block, returns first n elements from enum. + * With a block, passes elements to the block until the block + * returns nil or false, then stops iterating and returns an + * array of all prior elements. * - * a = [1, 2, 3, 4, 5] + * a = [1, 2, 3, 4, 5, 0] * * a.take(3) # => [1, 2, 3] * a.take {|i| i < 3 } # => [1, 2] @@ -1481,13 +1482,14 @@ drop_iter_i(VALUE i, VALUE *arg) * enum.drop {|arr| block } => array * * Without a block, drops first n elements from enum, and returns - * rest elements in an array. With a block, drops elements during block - * evaluation gives true. + * rest elements in an array. With a block, drops elements up to, but + * not including, the first element for which the block returns nil or false + * and returns an array containing the remaining elements. * - * a = [1, 2, 3, 4, 5] + * a = [1, 2, 3, 4, 5, 0] * - * a.drop(3) # => [4, 5] - * a.drop {|i| i < 3 } # => [3, 4, 5] + * a.drop(3) # => [4, 5, 0] + * a.drop {|i| i < 3 } # => [3, 4, 5, 0] * */ @@ -1525,8 +1527,10 @@ cycle_i(VALUE i, VALUE ary) * call-seq: * enum.cycle {|obj| block } * - * Calls block for each element of enumerable repeatedly - * forever. Enumerable#cycle saves elements in an internal array. + * Calls block for each element of enum repeatedly + * forever. Returns nil if and only if the collection is empty. + * Enumerable#cycle saves elements in an internal array so changes + * to enum after the first pass have no effect. * * a = ["a", "b", "c"] * a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever. @@ -1542,7 +1546,7 @@ enum_cycle(VALUE obj) RETURN_ENUMERATOR(obj, 0, 0); ary = rb_ary_new(); rb_block_call(obj, id_each, 0, 0, cycle_i, ary); - for (;;) { + while (RARRAY_LEN(ary) > 0) { for (i=0; i