ruby-changes:38410
From: sorah <ko1@a...>
Date: Thu, 14 May 2015 19:42:48 +0900 (JST)
Subject: [ruby-changes:38410] sorah:r50491 (trunk): * enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum):
sorah 2015-05-14 19:42:42 +0900 (Thu, 14 May 2015) New Revision: 50491 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50491 Log: * enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum): Implement Enumerable#grep_v. Modified files: trunk/ChangeLog trunk/NEWS trunk/enum.c trunk/test/ruby/test_enum.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50490) +++ ChangeLog (revision 50491) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Apr 8 19:18:02 2015 Shota Fukumori (sora_h) <her@s...> + + * 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@r...> * ext/pathname/lib/pathname.rb: Remove condition of RUBY_VERSION <= 1.9. Index: enum.c =================================================================== --- enum.c (revision 50490) +++ enum.c (revision 50491) @@ -45,7 +45,7 @@ grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg https://github.com/ruby/ruby/blob/trunk/enum.c#L45 struct MEMO *memo = MEMO_CAST(args); 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); } return Qnil; @@ -57,7 +57,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i https://github.com/ruby/ruby/blob/trunk/enum.c#L57 struct MEMO *memo = MEMO_CAST(args); 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)); } return Qnil; @@ -85,7 +85,33 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L85 enum_grep(VALUE obj, VALUE pat) { 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); @@ -3388,6 +3414,7 @@ Init_Enumerable(void) https://github.com/ruby/ruby/blob/trunk/enum.c#L3414 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, "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, "find", enum_find, -1); rb_define_method(rb_mEnumerable, "detect", enum_find, -1); Index: NEWS =================================================================== --- NEWS (revision 50490) +++ NEWS (revision 50491) @@ -15,6 +15,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L15 === 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) * Array Index: test/ruby/test_enum.rb =================================================================== --- test/ruby/test_enum.rb (revision 50490) +++ test/ruby/test_enum.rb (revision 50491) @@ -33,6 +33,18 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L33 $VERBOSE = @verbose 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 assert_equal([1, 2, 1, 2], @obj.grep(1..2)) a = [] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/