summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--NEWS4
-rw-r--r--enum.c33
-rw-r--r--test/ruby/test_enum.rb12
4 files changed, 51 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 377ec0b..01dc6a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/NEWS b/NEWS
index 2d3b4e3..993cee0 100644
--- a/NEWS
+++ b/NEWS
@@ -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
diff --git a/enum.c b/enum.c
index a49e4b0..a7182f9 100644
--- a/enum.c
+++ b/enum.c
@@ -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;
@@ -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;
@@ -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);
@@ -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);
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index 6961187..f774c42 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -33,6 +33,18 @@ class TestEnumerable < Test::Unit::TestCase
$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 = []