From: nobu@... Date: 2015-06-20T00:12:24+00:00 Subject: [ruby-core:69688] [Ruby trunk - Feature #11286] [PATCH] Add case equality arity to Enumerable's sequence predicates. Issue #11286 has been updated by Nobuyoshi Nakada. D.E. Akers wrote: > I assumed `*argv` would resolve to `Qnil` in the case of no arguments, but this is indeed not the case. Nobody guarantees it. And argument check should be before making a memo object, I think. ~~~diff diff --git a/enum.c b/enum.c index c5b9d77..d6f11e5 100644 --- a/enum.c +++ b/enum.c @@ -1045,6 +1045,8 @@ enum_sort_by(VALUE obj) #define ENUMFUNC(name, argc) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i +#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0)) + #define DEFINE_ENUMFUNCS(name) \ static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \ \ @@ -1104,8 +1106,7 @@ DEFINE_ENUMFUNCS(all) static VALUE enum_all(int argc, VALUE *argv, VALUE obj) { - struct MEMO *memo = MEMO_NEW(Qtrue, argc ? *argv : 0, 0); - rb_check_arity(argc, 0, 1); + struct MEMO *memo = MEMO_ENUM_NEW(Qtrue); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all, argc), (VALUE)memo); return memo->v1; } @@ -1145,8 +1146,7 @@ DEFINE_ENUMFUNCS(any) static VALUE enum_any(int argc, VALUE *argv, VALUE obj) { - struct MEMO *memo = MEMO_NEW(Qfalse, argc ? *argv : 0, 0); - rb_check_arity(argc, 0, 1); + struct MEMO *memo = MEMO_ENUM_NEW(Qfalse); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(any, argc), (VALUE)memo); return memo->v1; } @@ -1438,8 +1438,7 @@ DEFINE_ENUMFUNCS(none) static VALUE enum_none(int argc, VALUE *argv, VALUE obj) { - struct MEMO *memo = MEMO_NEW(Qtrue, argc ? *argv : 0, 0); - rb_check_arity(argc, 0, 1); + struct MEMO *memo = MEMO_ENUM_NEW(Qtrue); rb_block_call(obj, id_each, 0, 0, ENUMFUNC(none, argc), (VALUE)memo); return memo->v1; } ~~~ ---------------------------------------- Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates. https://bugs.ruby-lang.org/issues/11286#change-53063 * Author: D.E. Akers * Status: Open * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- ## Proposal It is proposed that `Enumerable`'s sequence predicates (`#all?`, `#any?`, `#none?`, and `#one?`) be augmented to return, in the case of a single argument, whether their query holds when each element is supplied to the argument's `#===` method. ## Rationale `Enumerable#grep` filters by case equality, allowing us to write very natural and expressive code: ```ruby strs.select { |str| /foo/ === str } strs.grep(/foo/) nums.select { |num| (5..10) === num } nums.grep(5..10) ``` In addition to taking advantage of the versatility of case equality, it lets us do away with the syntactic noise incurred by opening a block. `#grep` is a very nice method! Let's make `#all?` and friends more like `#grep`. ---Files-------------------------------- 0001-enum.c-add-case-equality-arity-to-sequence-predicates.patch (10 KB) case_equality_sequence_predicates-check_argc_before_deref.patch (10 KB) -- https://bugs.ruby-lang.org/