From bd372844bd9e7d4d96ba770bbfee47e8ffe3dc10 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 6 Mar 2014 19:59:15 -0800 Subject: [PATCH] Enumerable#select to take args and use === --- enum.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/enum.c b/enum.c index cd2d49d..c7526e4 100644 --- a/enum.c +++ b/enum.c @@ -299,7 +299,7 @@ enum_find_index(int argc, VALUE *argv, VALUE obj) } static VALUE -find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary)) +find_all_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary)) { ENUM_WANT_SVALUE(); @@ -310,6 +310,22 @@ enum_find_index(int argc, VALUE *argv, VALUE obj) } static VALUE +find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args)) +{ + NODE *memo = RNODE(args); + VALUE ary = memo->u1.value; + long j; + ENUM_WANT_SVALUE(); + + for (j=0; ju2.value, i); + } + } + return Qnil; +} + +static VALUE enum_size(VALUE self, VALUE args, VALUE eobj) { VALUE r; @@ -338,14 +354,27 @@ enum_size(VALUE self, VALUE args, VALUE eobj) */ static VALUE -enum_find_all(VALUE obj) +enum_find_all(int argc, VALUE *argv, VALUE obj) { VALUE ary; + NODE *memo; + VALUE items = Qnil; - RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size); + if (argc == 0) { + RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); - ary = rb_ary_new(); - rb_block_call(obj, id_each, 0, 0, find_all_i, ary); + ary = rb_ary_new(); + rb_block_call(obj, id_each, 0, 0, find_all_iter_i, ary); + } else { + rb_scan_args(argc, argv, "*", &items); + + if (rb_block_given_p()) + rb_warn("given block not used"); + + ary = rb_ary_new(); + memo = NEW_MEMO(items, ary, 0); + rb_block_call(obj, id_each, 0, 0, find_all_i, (VALUE)memo); + } return ary; } @@ -3052,8 +3081,8 @@ enum_slice_before(int argc, VALUE *argv, VALUE enumerable) rb_define_method(rb_mEnumerable, "find", enum_find, -1); rb_define_method(rb_mEnumerable, "detect", enum_find, -1); rb_define_method(rb_mEnumerable, "find_index", enum_find_index, -1); - rb_define_method(rb_mEnumerable, "find_all", enum_find_all, 0); - rb_define_method(rb_mEnumerable, "select", enum_find_all, 0); + rb_define_method(rb_mEnumerable, "find_all", enum_find_all, -1); + rb_define_method(rb_mEnumerable, "select", enum_find_all, -1); rb_define_method(rb_mEnumerable, "reject", enum_reject, 0); rb_define_method(rb_mEnumerable, "collect", enum_collect, 0); rb_define_method(rb_mEnumerable, "map", enum_collect, 0); -- 1.8.5.5