Skip to content

Commit

Permalink
* enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum):
Browse files Browse the repository at this point in the history
  Implement Enumerable#grep_v.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50491 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
sorah committed May 14, 2015
1 parent 180293a commit 71588d1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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>

* ext/pathname/lib/pathname.rb: Remove condition of RUBY_VERSION <= 1.9.
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ with all sufficient information, see the ChangeLog file.

=== 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
Expand Down
33 changes: 30 additions & 3 deletions enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
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;
Expand All @@ -57,7 +57,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
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;
Expand Down Expand Up @@ -85,7 +85,33 @@ static VALUE
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);

Expand Down Expand Up @@ -3388,6 +3414,7 @@ Init_Enumerable(void)
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);
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def teardown
$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 = []
Expand Down

0 comments on commit 71588d1

Please sign in to comment.