mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum):
Implement Enumerable#grep_v. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50491 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
180293ac89
commit
71588d17e5
4 changed files with 51 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Wed Apr 8 19:18:02 2015 Shota Fukumori (sora_h) <her@sorah.jp>
|
||||||
|
|
||||||
|
* enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum):
|
||||||
|
Implement Enumerable#grep_v.
|
||||||
|
|
||||||
Thu May 14 15:54:13 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
Thu May 14 15:54:13 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||||
|
|
||||||
* ext/pathname/lib/pathname.rb: Remove condition of RUBY_VERSION <= 1.9.
|
* ext/pathname/lib/pathname.rb: Remove condition of RUBY_VERSION <= 1.9.
|
||||||
|
|
4
NEWS
4
NEWS
|
@ -15,6 +15,10 @@ with all sufficient information, see the ChangeLog file.
|
||||||
|
|
||||||
=== Core classes updates (outstanding ones only)
|
=== Core classes updates (outstanding ones only)
|
||||||
|
|
||||||
|
* Enumerable
|
||||||
|
|
||||||
|
* Enumerable#grep_v is added as inversed version of Enumerable#grep.
|
||||||
|
|
||||||
=== Core classes compatibility issues (excluding feature bug fixes)
|
=== Core classes compatibility issues (excluding feature bug fixes)
|
||||||
|
|
||||||
* Array
|
* Array
|
||||||
|
|
33
enum.c
33
enum.c
|
@ -45,7 +45,7 @@ grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
struct MEMO *memo = MEMO_CAST(args);
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i))) {
|
if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i)) == RTEST(memo->u3.value)) {
|
||||||
rb_ary_push(memo->v2, i);
|
rb_ary_push(memo->v2, i);
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -57,7 +57,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
struct MEMO *memo = MEMO_CAST(args);
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i))) {
|
if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i)) == RTEST(memo->u3.value)) {
|
||||||
rb_ary_push(memo->v2, rb_yield(i));
|
rb_ary_push(memo->v2, rb_yield(i));
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -85,7 +85,33 @@ static VALUE
|
||||||
enum_grep(VALUE obj, VALUE pat)
|
enum_grep(VALUE obj, VALUE pat)
|
||||||
{
|
{
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary = rb_ary_new();
|
||||||
struct MEMO *memo = MEMO_NEW(pat, ary, 0);
|
struct MEMO *memo = MEMO_NEW(pat, ary, Qtrue);
|
||||||
|
|
||||||
|
rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
|
||||||
|
|
||||||
|
return ary;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* enum.grep_v(pattern) -> array
|
||||||
|
* enum.grep_v(pattern) { |obj| block } -> array
|
||||||
|
*
|
||||||
|
* Inversed version of Enumerable#grep.
|
||||||
|
* Returns an array of every element in <i>enum</i> for which
|
||||||
|
* not <code>Pattern === element</code>.
|
||||||
|
*
|
||||||
|
* (1..10).grep_v 2..5 #=> [1, 6, 7, 8, 9, 10]
|
||||||
|
* res =(1..10).grep_v(2..5) { |v| v * 2 }
|
||||||
|
* res #=> [1, 12, 14, 16, 18, 20]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
enum_grep_v(VALUE obj, VALUE pat)
|
||||||
|
{
|
||||||
|
VALUE ary = rb_ary_new();
|
||||||
|
struct MEMO *memo = MEMO_NEW(pat, ary, Qfalse);
|
||||||
|
|
||||||
rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
|
||||||
|
|
||||||
|
@ -3388,6 +3414,7 @@ Init_Enumerable(void)
|
||||||
rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
|
rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
|
||||||
rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
|
rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
|
||||||
rb_define_method(rb_mEnumerable, "grep", enum_grep, 1);
|
rb_define_method(rb_mEnumerable, "grep", enum_grep, 1);
|
||||||
|
rb_define_method(rb_mEnumerable, "grep_v", enum_grep_v, 1);
|
||||||
rb_define_method(rb_mEnumerable, "count", enum_count, -1);
|
rb_define_method(rb_mEnumerable, "count", enum_count, -1);
|
||||||
rb_define_method(rb_mEnumerable, "find", enum_find, -1);
|
rb_define_method(rb_mEnumerable, "find", enum_find, -1);
|
||||||
rb_define_method(rb_mEnumerable, "detect", enum_find, -1);
|
rb_define_method(rb_mEnumerable, "detect", enum_find, -1);
|
||||||
|
|
|
@ -33,6 +33,18 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
$VERBOSE = @verbose
|
$VERBOSE = @verbose
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_grep_v
|
||||||
|
assert_equal([3], @obj.grep_v(1..2))
|
||||||
|
a = []
|
||||||
|
@obj.grep_v(2) {|x| a << x }
|
||||||
|
assert_equal([1, 3, 1], a)
|
||||||
|
|
||||||
|
a = []
|
||||||
|
lambda = ->(x, i) {a << [x, i]}
|
||||||
|
@obj.each_with_index.grep_v(proc{|x,i|x!=2}, &lambda)
|
||||||
|
assert_equal([[2, 1], [2, 4]], a)
|
||||||
|
end
|
||||||
|
|
||||||
def test_grep
|
def test_grep
|
||||||
assert_equal([1, 2, 1, 2], @obj.grep(1..2))
|
assert_equal([1, 2, 1, 2], @obj.grep(1..2))
|
||||||
a = []
|
a = []
|
||||||
|
|
Loading…
Add table
Reference in a new issue