Skip to content

Commit

Permalink
* enum.c (enum_to_a): Pass arguments through to #each().
Browse files Browse the repository at this point in the history
  (enum_sort): Follow the enum_to_a signature change.
  (enum_reverse_each): Add #reverse_each().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16622 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
knu committed May 27, 2008
1 parent abfb5cc commit fc19d23
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Tue May 27 13:14:53 2008 Akinori MUSHA <knu@iDaemons.org>

* enum.c (enum_to_a): Pass arguments through to #each().
(enum_sort): Follow the enum_to_a signature change.
(enum_reverse_each): Add #reverse_each().

Tue May 27 13:12:37 2008 Akinori MUSHA <knu@iDaemons.org>

* io.c (Init_IO): Define ARGF.{lines,bytes,chars}.
Expand Down
36 changes: 31 additions & 5 deletions enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ enum_collect(VALUE obj)
* { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
*/
static VALUE
enum_to_a(VALUE obj)
enum_to_a(int argc, VALUE *argv, VALUE obj)
{
VALUE ary = rb_ary_new();

rb_block_call(obj, id_each, 0, 0, collect_all, ary);
rb_block_call(obj, id_each, argc, argv, collect_all, ary);

return ary;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ enum_first(int argc, VALUE *argv, VALUE obj)
static VALUE
enum_sort(VALUE obj)
{
return rb_ary_sort(enum_to_a(obj));
return rb_ary_sort(enum_to_a(0, 0, obj));
}

static VALUE
Expand Down Expand Up @@ -1404,6 +1404,31 @@ enum_each_with_index(int argc, VALUE *argv, VALUE obj)
}


/*
* call-seq:
* enum.reverse_each {|item| block }
*
* Traverses <i>enum</i> in reverse order.
*/

static VALUE
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
{
VALUE ary;
long i;

RETURN_ENUMERATOR(obj, argc, argv);

ary = enum_to_a(argc, argv, obj);

for (i = RARRAY_LEN(ary); --i >= 0; ) {
rb_yield(RARRAY_PTR(ary)[i]);
}

return obj;
}


static VALUE
zip_ary(VALUE val, NODE *memo, int argc, VALUE *argv)
{
Expand Down Expand Up @@ -1756,8 +1781,8 @@ Init_Enumerable(void)
{
rb_mEnumerable = rb_define_module("Enumerable");

rb_define_method(rb_mEnumerable, "to_a", enum_to_a, 0);
rb_define_method(rb_mEnumerable, "entries", enum_to_a, 0);
rb_define_method(rb_mEnumerable, "to_a", enum_to_a, -1);
rb_define_method(rb_mEnumerable, "entries", enum_to_a, -1);

rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
Expand Down Expand Up @@ -1789,6 +1814,7 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "member?", enum_member, 1);
rb_define_method(rb_mEnumerable, "include?", enum_member, 1);
rb_define_method(rb_mEnumerable, "each_with_index", enum_each_with_index, -1);
rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1);
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, 1);
rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);
Expand Down

0 comments on commit fc19d23

Please sign in to comment.