Project

General

Profile

Feature #11049 ยป grepv.patch

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

View differences:

ChangeLog
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.
Wed Apr 8 14:57:06 2015 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/win32.c (rb_w32_wreadlink): should treat junctions like as
NEWS
=== 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
enum.c
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;
......
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;
......
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);
......
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);
test/ruby/test_enum.rb
assert_equal("", warn)
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 = []
......
assert_equal([[2, 1], [2, 4]], a)
end
def test_count
assert_equal(5, @obj.count)
assert_equal(2, @obj.count(1))
    (1-1/1)