mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (rb_ary_take, rb_ary_take_while, rb_ary_drop,
rb_ary_drop_while): new method. [ruby-dev:34067] * test/ruby/test_array.rb: add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15791 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c97735b53c
commit
89afc5431b
3 changed files with 118 additions and 0 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Mon Mar 17 22:23:54 2008 Yusuke Endoh <mame@tsg.ne.jp>
|
||||||
|
|
||||||
|
* array.c (rb_ary_take, rb_ary_take_while, rb_ary_drop,
|
||||||
|
rb_ary_drop_while): new methods. [ruby-dev:34067]
|
||||||
|
|
||||||
|
* test/ruby/test_array.rb: add tests for above.
|
||||||
|
|
||||||
Mon Mar 17 17:11:13 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Mar 17 17:11:13 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* misc/ruby-mode.el (ruby-mode): should use `run-mode-hooks' instead
|
* misc/ruby-mode.el (ruby-mode): should use `run-mode-hooks' instead
|
||||||
|
|
95
array.c
95
array.c
|
@ -3228,6 +3228,96 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* ary.take(n) => array
|
||||||
|
*
|
||||||
|
* Returns first n elements from <i>ary</i>.
|
||||||
|
*
|
||||||
|
* a = [1, 2, 3, 4, 5, 0]
|
||||||
|
* a.take(3) # => [1, 2, 3]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_ary_take(VALUE obj, VALUE n)
|
||||||
|
{
|
||||||
|
return rb_ary_subseq(obj, 0, FIX2LONG(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* ary.take_while {|arr| block } => array
|
||||||
|
*
|
||||||
|
* 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, 0]
|
||||||
|
* a.take_while {|i| i < 3 } # => [1, 2]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_ary_take_while(VALUE ary)
|
||||||
|
{
|
||||||
|
VALUE result;
|
||||||
|
long i;
|
||||||
|
|
||||||
|
RETURN_ENUMERATOR(ary, 0, 0);
|
||||||
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
||||||
|
if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
|
||||||
|
}
|
||||||
|
return rb_ary_take(ary, LONG2FIX(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* ary.drop(n) => array
|
||||||
|
*
|
||||||
|
* Drops first n elements from <i>ary</i>, and returns rest elements
|
||||||
|
* in an array.
|
||||||
|
*
|
||||||
|
* a = [1, 2, 3, 4, 5, 0]
|
||||||
|
* a.drop(3) # => [4, 5, 0]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_ary_drop(VALUE ary, VALUE n)
|
||||||
|
{
|
||||||
|
VALUE result;
|
||||||
|
|
||||||
|
result = rb_ary_subseq(ary, FIX2LONG(n), RARRAY_LEN(ary));
|
||||||
|
if (result == Qnil) result = rb_ary_new();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* ary.drop_while {|arr| block } => array
|
||||||
|
*
|
||||||
|
* 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, 0]
|
||||||
|
* a.drop_while {|i| i < 3 } # => [3, 4, 5, 0]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_ary_drop_while(VALUE ary)
|
||||||
|
{
|
||||||
|
VALUE result;
|
||||||
|
long i;
|
||||||
|
|
||||||
|
RETURN_ENUMERATOR(ary, 0, 0);
|
||||||
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
||||||
|
if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
|
||||||
|
}
|
||||||
|
return rb_ary_drop(ary, LONG2FIX(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Arrays are ordered, integer-indexed collections of any object.
|
/* Arrays are ordered, integer-indexed collections of any object.
|
||||||
|
@ -3332,5 +3422,10 @@ Init_Array(void)
|
||||||
rb_define_method(rb_cArray, "combination", rb_ary_combination, 1);
|
rb_define_method(rb_cArray, "combination", rb_ary_combination, 1);
|
||||||
rb_define_method(rb_cArray, "product", rb_ary_product, -1);
|
rb_define_method(rb_cArray, "product", rb_ary_product, -1);
|
||||||
|
|
||||||
|
rb_define_method(rb_cArray, "take", rb_ary_take, 1);
|
||||||
|
rb_define_method(rb_cArray, "take_while", rb_ary_take_while, 0);
|
||||||
|
rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
|
||||||
|
rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
|
||||||
|
|
||||||
id_cmp = rb_intern("<=>");
|
id_cmp = rb_intern("<=>");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1229,4 +1229,20 @@ class TestArray < Test::Unit::TestCase
|
||||||
assert_equal(@cls[].permutation(0).to_a, @cls[[]])
|
assert_equal(@cls[].permutation(0).to_a, @cls[[]])
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_take
|
||||||
|
assert_equal([1,2,3], [1,2,3,4,5,0].take(3))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_take_while
|
||||||
|
assert_equal([1,2], [1,2,3,4,5,0].take_while {|i| i < 3 })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_drop
|
||||||
|
assert_equal([4,5,0], [1,2,3,4,5,0].drop(3))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_drop_while
|
||||||
|
assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue