diff options
author | 卜部昌平 <[email protected]> | 2020-08-13 11:44:15 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2020-12-22 13:52:03 +0900 |
commit | fa356a798aefc20725467d4fad02df8325d63e71 (patch) | |
tree | 9150cab2aaa1fbb14e5461d0166f8a09da2a5209 /enumerator.c | |
parent | 7204b81bcb13f84ca3cc34384d205565fd5ad381 (diff) |
Enumerator.new: raise unless block given
Has been deprecated since c73b6bd7ebd01133538c645566944132dbde4d13.
[Feature #17116] [ruby-dev:50945]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3968
Diffstat (limited to 'enumerator.c')
-rw-r--r-- | enumerator.c | 66 |
1 files changed, 23 insertions, 43 deletions
diff --git a/enumerator.c b/enumerator.c index fe5f054c74..b4a7cb588e 100644 --- a/enumerator.c +++ b/enumerator.c @@ -420,15 +420,31 @@ enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, const VALUE *ar return enum_obj; } +static VALUE +convert_to_feasible_size_value(VALUE obj) +{ + if (NIL_P(obj)) { + return obj; + } + else if (rb_respond_to(obj, id_call)) { + return obj; + } + else if (RB_FLOAT_TYPE_P(obj) && RFLOAT_VALUE(obj) == HUGE_VAL) { + return obj; + } + else { + return rb_to_int(obj); + } +} + /* * call-seq: * Enumerator.new(size = nil) { |yielder| ... } - * Enumerator.new(obj, method = :each, *args) * * Creates a new Enumerator object, which can be used as an * Enumerable. * - * In the first form, iteration is defined by the given block, in + * Iteration is defined by the given block, in * which a "yielder" object, given as block parameter, can be used to * yield a value by calling the +yield+ method (aliased as <code><<</code>): * @@ -445,52 +461,16 @@ enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, const VALUE *ar * The optional parameter can be used to specify how to calculate the size * in a lazy fashion (see Enumerator#size). It can either be a value or * a callable object. - * - * In the deprecated second form, a generated Enumerator iterates over the - * given object using the given method with the given arguments passed. - * - * Use of this form is discouraged. Use Object#enum_for or Object#to_enum - * instead. - * - * e = Enumerator.new(ObjectSpace, :each_object) - * #-> ObjectSpace.enum_for(:each_object) - * - * e.select { |obj| obj.is_a?(Class) } # => array of all classes - * */ static VALUE enumerator_initialize(int argc, VALUE *argv, VALUE obj) { - VALUE recv, meth = sym_each; - VALUE size = Qnil; - int kw_splat = 0; - - if (rb_block_given_p()) { - rb_check_arity(argc, 0, 1); - recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc()); - if (argc) { - if (NIL_P(argv[0]) || rb_respond_to(argv[0], id_call) || - (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == HUGE_VAL)) { - size = argv[0]; - } - else { - size = rb_to_int(argv[0]); - } - argc = 0; - } - } - else { - rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); - rb_warn_deprecated("Enumerator.new without a block", "Object#to_enum"); - recv = *argv++; - if (--argc) { - meth = *argv++; - --argc; - } - kw_splat = rb_keyword_given_p(); - } + VALUE iter = rb_block_proc(); + VALUE recv = generator_init(generator_allocate(rb_cGenerator), iter); + VALUE arg0 = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil; + VALUE size = convert_to_feasible_size_value(arg0); - return enumerator_init(obj, recv, meth, argc, argv, 0, size, kw_splat); + return enumerator_init(obj, recv, sym_each, 0, 0, 0, size, false); } /* :nodoc: */ |