зеркало из https://github.com/github/ruby.git
* enumerator.c (enumerator_with_index): receives one argument which
represents a start offset. [ruby-dev:37921] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22134 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
e5f588c83d
Коммит
3e11901f1c
|
@ -1,3 +1,8 @@
|
|||
Sun Feb 8 23:37:17 2009 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* enumerator.c (enumerator_with_index): receives one argument which
|
||||
represents a start offset. [ruby-dev:37921]
|
||||
|
||||
Sun Feb 8 23:28:05 2009 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* include/ruby/st.h, st.c: order entries by a linked list instead of
|
||||
|
|
37
enumerator.c
37
enumerator.c
|
@ -406,31 +406,50 @@ enumerator_with_index_i(VALUE val, VALUE *memo)
|
|||
|
||||
/*
|
||||
* call-seq:
|
||||
* e.with_index {|(*args), idx| ... }
|
||||
* e.with_index(offset = 0) {|(*args), idx| ... }
|
||||
* e.with_index
|
||||
*
|
||||
* Iterates the given block for each element with an index, which
|
||||
* start from 0. If no block is given, returns an enumerator.
|
||||
* starts from +offset+. If no block is given, returns an enumerator.
|
||||
*
|
||||
*/
|
||||
static VALUE
|
||||
enumerator_with_index(VALUE obj)
|
||||
enumerator_with_index(int argc, VALUE *argv, VALUE obj)
|
||||
{
|
||||
struct enumerator *e;
|
||||
VALUE memo = 0;
|
||||
int argc = 0;
|
||||
VALUE *argv = 0;
|
||||
VALUE memo;
|
||||
|
||||
RETURN_ENUMERATOR(obj, 0, 0);
|
||||
rb_scan_args(argc, argv, "01", &memo);
|
||||
RETURN_ENUMERATOR(obj, argc, argv);
|
||||
memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo);
|
||||
e = enumerator_ptr(obj);
|
||||
if (e->args) {
|
||||
argc = RARRAY_LEN(e->args);
|
||||
argv = RARRAY_PTR(e->args);
|
||||
}
|
||||
else {
|
||||
argc = 0;
|
||||
argv = NULL;
|
||||
}
|
||||
return rb_block_call(e->obj, e->meth, argc, argv,
|
||||
enumerator_with_index_i, (VALUE)&memo);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* e.each_with_index {|(*args), idx| ... }
|
||||
* e.each_with_index
|
||||
*
|
||||
* Same as Enumeartor#with_index, except each_with_index does not
|
||||
* receive an offset argument.
|
||||
*
|
||||
*/
|
||||
static VALUE
|
||||
enumerator_each_with_index(VALUE obj)
|
||||
{
|
||||
return enumerator_with_index(0, NULL, obj);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
enumerator_with_object_i(VALUE val, VALUE memo)
|
||||
{
|
||||
|
@ -841,9 +860,9 @@ Init_Enumerator(void)
|
|||
rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1);
|
||||
rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
|
||||
rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
|
||||
rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0);
|
||||
rb_define_method(rb_cEnumerator, "each_with_index", enumerator_each_with_index, 0);
|
||||
rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1);
|
||||
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
|
||||
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1);
|
||||
rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
|
||||
rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
|
||||
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
|
||||
|
|
|
@ -94,6 +94,7 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
|
||||
def test_with_index
|
||||
assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a)
|
||||
assert_equal([[1,5],[2,6],[3,7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a)
|
||||
end
|
||||
|
||||
def test_with_object
|
||||
|
|
Загрузка…
Ссылка в новой задаче