Project

General

Profile

Actions

Misc #9136

open

Deprecated Enumerator.new(object, method) bad for BasicObject

Added by atlas (Atlas Prime) over 10 years ago. Updated over 10 years ago.

Status:
Assigned
Assignee:
[ruby-core:58463]

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

Updated by atlas (Atlas Prime) over 10 years ago

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.

Updated by atlas (Atlas Prime) over 10 years ago

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?

Updated by zzak (zzak _) over 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to zzak (zzak _)

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

=begin
FYI, (({Kernel.instance_method(:to_enum).bind(basic_object).call})) can work since 2.0.
=end

Updated by atlas (Atlas Prime) over 10 years ago

@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) ?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0