Project

General

Profile

Feature #11049 ยป grepv.patch

sorah (Sorah Fukumori), 04/08/2015 10:23 AM

View differences:

ChangeLog
1
Wed Apr  8 19:18:02 2015  Shota Fukumori (sora_h)  <her@sorah.jp>
2

  
3
	* enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum): Implement Enumerable#grep_v.
4

  
1 5
Wed Apr  8 14:57:06 2015  NAKAMURA Usaku  <usa@ruby-lang.org>
2 6

  
3 7
	* win32/win32.c (rb_w32_wreadlink): should treat junctions like as
NEWS
15 15

  
16 16
=== Core classes updates (outstanding ones only)
17 17

  
18
* Enumerable
19

  
20
  * Enumerable#grep_v is added as inversed version of Enumerable#grep.
21

  
18 22
=== Core classes compatibility issues (excluding feature bug fixes)
19 23

  
20 24
* Array
enum.c
45 45
    struct MEMO *memo = MEMO_CAST(args);
46 46
    ENUM_WANT_SVALUE();
47 47

  
48
    if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i))) {
48
    if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i)) == RTEST(memo->u3.value)) {
49 49
	rb_ary_push(memo->v2, i);
50 50
    }
51 51
    return Qnil;
......
57 57
    struct MEMO *memo = MEMO_CAST(args);
58 58
    ENUM_WANT_SVALUE();
59 59

  
60
    if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i))) {
60
    if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i)) == RTEST(memo->u3.value)) {
61 61
	rb_ary_push(memo->v2, rb_yield(i));
62 62
    }
63 63
    return Qnil;
......
85 85
enum_grep(VALUE obj, VALUE pat)
86 86
{
87 87
    VALUE ary = rb_ary_new();
88
    struct MEMO *memo = MEMO_NEW(pat, ary, 0);
88
    struct MEMO *memo = MEMO_NEW(pat, ary, Qtrue);
89

  
90
    rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
91

  
92
    return ary;
93
}
94

  
95
/*
96
 *  call-seq:
97
 *     enum.grep_v(pattern)                  -> array
98
 *     enum.grep_v(pattern) { |obj| block }  -> array
99
 *
100
 *  Inversed version of Enumerable#grep.
101
 *  Returns an array of every element in <i>enum</i> for which
102
 *  not <code>Pattern === element</code>.
103
 *
104
 *     (1..10).grep_v 2..5   #=> [1, 6, 7, 8, 9, 10]
105
 *     res =(1..10).grep_v(2..5) { |v| v * 2 }
106
 *     res                    #=> [1, 12, 14, 16, 18, 20]
107
 *
108
 */
109

  
110
static VALUE
111
enum_grep_v(VALUE obj, VALUE pat)
112
{
113
    VALUE ary = rb_ary_new();
114
    struct MEMO *memo = MEMO_NEW(pat, ary, Qfalse);
89 115

  
90 116
    rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
91 117

  
......
3388 3414
    rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
3389 3415
    rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
3390 3416
    rb_define_method(rb_mEnumerable, "grep", enum_grep, 1);
3417
    rb_define_method(rb_mEnumerable, "grep_v", enum_grep_v, 1);
3391 3418
    rb_define_method(rb_mEnumerable, "count", enum_count, -1);
3392 3419
    rb_define_method(rb_mEnumerable, "find", enum_find, -1);
3393 3420
    rb_define_method(rb_mEnumerable, "detect", enum_find, -1);
test/ruby/test_enum.rb
47 47
    assert_equal("", warn)
48 48
  end
49 49

  
50
  def test_grep_v
51
    assert_equal([3], @obj.grep_v(1..2))
52
    a = []
53
    @obj.grep_v(2) {|x| a << x }
54
    assert_equal([1, 3, 1], a)
55

  
56
    a = []
57
    lambda = ->(x, i) {a << [x, i]}
58
    @obj.each_with_index.grep_v(proc{|x,i|x!=2}, &lambda)
59
    assert_equal([[2, 1], [2, 4]], a)
60
  end
61

  
50 62
  def test_grep
51 63
    assert_equal([1, 2, 1, 2], @obj.grep(1..2))
52 64
    a = []
......
63 75
    assert_equal([[2, 1], [2, 4]], a)
64 76
  end
65 77

  
78

  
66 79
  def test_count
67 80
    assert_equal(5, @obj.count)
68 81
    assert_equal(2, @obj.count(1))