Misc #9136
open
Deprecated Enumerator.new(object, method) bad for BasicObject
Added by atlas (Atlas Prime) about 11 years ago.
Updated about 11 years ago.
Description
=begin
Documentation it says:
In the second, deprecated, 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 Kernel#enum_for or Kernel#to_enum instead.
e = Enumerator.new(ObjectSpace, :each_object)
#-> ObjectSpace.enum_for(:each_object)
But (({#enum_for})) and (({#to_enum})) are not available to subclasses of (({BasicObject})). In fact, I was defining (({#to_enum})) for a class that is a subclass of (({BasicObject})), and now I get warning of deprecation.
=end
Documentation it says:
((*In the second, deprecated, 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 Kernel#enum_for or Kernel#to_enum instead.
e = Enumerator.new(ObjectSpace, :each_object)
#-> ObjectSpace.enum_for(:each_object)
But #enum_for and #to_enum are not available to subclasses of BasicObject. In fact, I was defining #to_enum for a class that is a subclass of BasicObject, and now I get warning of deprecation.
P.S. This interface for bug reports is buggy (and I see no way to fix what I submitted). Not to offend, but why do it when there is GitHub issues?
- Status changed from Open to Assigned
- Assignee set to zzak (zzak _)
- Description updated (diff)
=begin
FYI, (({Kernel.instance_method(:to_enum).bind(basic_object).call})) can work since 2.0.
=end
@nobu (Nobuyoshi Nakada) I found that out but it seems kind of hackish way to have to do it. (Also a little counter-intuitive since Kernel is not in BasicObject). I ended up with:
def to_enum(meth=:each)
Enumerator.new{ |y| __send__(meth) { |*a| y.yield *a } }
end
Which seems to work, though I do not like so much using send.
Is it not good to have Enumerator.new(object, method) ? Maybe it can be different constructor, e.g. Enumerator.for(object, method) ?
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0