* enum.c (enum_cycle): new method to cycle enumerable forever.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-08-06 16:26:17 +00:00
Родитель 5956c7ab3e
Коммит b3e977a4c0
4 изменённых файлов: 72 добавлений и 3 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Tue Aug 7 01:15:24 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (enum_cycle): new method to cycle enumerable forever.
Tue Aug 7 00:05:38 2007 Keiju Ishitsuka <keiju@ruby-lang.org>
* irb/ruby-lex.rb: support for '\c'. [ruby-talk:263508]

26
array.c
Просмотреть файл

@ -2951,6 +2951,31 @@ rb_ary_choice(VALUE ary)
}
/*
* call-seq:
* ary.cycle {|obj| block }
*
* Calls <i>block</i> repeatedly forever.
*
* a = ["a", "b", "c"]
* a.each {|x| puts x } # print, a, b, c, a, b, c,.. forever.
*
*/
static VALUE
rb_ary_cycle(VALUE ary)
{
long i;
RETURN_ENUMERATOR(ary, 0, 0);
for (;;) {
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(RARRAY_PTR(ary)[i]);
}
}
return Qnil;
}
/* Arrays are ordered, integer-indexed collections of any object.
* Array indexing starts at 0, as in C or Java. A negative index is
* assumed to be relative to the end of the array---that is, an index of -1
@ -3048,6 +3073,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);
rb_define_method(rb_cArray, "choice", rb_ary_choice, 0);
rb_define_method(rb_cArray, "cycle", rb_ary_cycle, 0);
id_cmp = rb_intern("<=>");
}

39
enum.c
Просмотреть файл

@ -1526,6 +1526,44 @@ enum_drop(int argc, VALUE *argv, VALUE obj)
return args[0];
}
static VALUE
cycle_i(VALUE i, VALUE ary)
{
rb_ary_push(ary, i);
rb_yield(i);
return Qnil;
}
/*
* call-seq:
* enum.cycle {|obj| block }
*
* Calls <i>block</i> for each element of enumerable repeatedly
* forever. Enumerable#cycle saves elements in an internal array.
*
* a = ["a", "b", "c"]
* a.each {|x| puts x } # print, a, b, c, a, b, c,.. forever.
*
*/
static VALUE
enum_cycle(VALUE obj)
{
VALUE ary;
long i;
RETURN_ENUMERATOR(obj, 0, 0);
ary = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, cycle_i, ary);
for (;;) {
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(RARRAY_PTR(ary)[i]);
}
}
return Qnil;
}
/*
* The <code>Enumerable</code> mixin provides collection classes with
* several traversal and searching methods, and with the ability to
@ -1578,6 +1616,7 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, -1);
rb_define_method(rb_mEnumerable, "drop", enum_drop, -1);
rb_define_method(rb_mEnumerable, "cycle", enum_cycle, 0);
id_eqq = rb_intern("===");
id_each = rb_intern("each");

Просмотреть файл

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2007-08-06"
#define RUBY_RELEASE_DATE "2007-08-07"
#define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20070806
#define RUBY_RELEASE_CODE 20070807
#define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 8
#define RUBY_RELEASE_DAY 6
#define RUBY_RELEASE_DAY 7
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];