Bug #8641

Enumerator size argument is either mis-documented or should accept any callable

Added by Avdi Grimm 9 months ago. Updated 8 months ago.

[ruby-core:56032]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune
Category:core
Target version:2.1.0
ruby -v:ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

I ran into this while wrapping Enumerators around Queues. The Enumerator docs state:

  • 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.

However:

require 'thread'
q = Queue.new
eq = Enumerator.new(q.method(:size)) do |y|
loop do
y << q.pop
end
end
eq.size # =>
# ~> -:3:in initialize': no implicit conversion of Method into Integer (TypeError)
# ~> from -:3:in
new'
# ~> from -:3:in `'

It looks like the size can't be any callable; it must be Proc a or an Integer. The code uses rbobjis_proc().

Instinctively I'd say that the documentation has it right and it ought to take any callable. But if there's some reason it's limited to Procs, the docs should be changed.

If anyone can point me in the right direction I'm happy to do the legwork on this.

Associated revisions

Revision 42698
Added by Marc-Andre Lafortune 8 months ago

  • enumerator.c: Allow Enumerator size argument to be any callable.
    Patch by Avdi Grimm. [bug #8641] [fix GH-362]

  • test/ruby/test_enumerator.rb: Test for above

History

#1 Updated by Marc-Andre Lafortune 9 months ago

  • Category set to core
  • Assignee set to Marc-Andre Lafortune
  • Target version set to 2.1.0

Instinctively I'd say that the documentation has it right and it ought to take any callable.

Agreed, I'll address this.

Thanks

#2 Updated by Avdi Grimm 9 months ago

Thanks Marc. Because I really really want to learn how to do this stuff, I went ahead cargo-culted a fix here: https://github.com/ruby/ruby/pull/362

This is my very first attempt at hacking Ruby C code. I don't expect this to be merged in, but I would LOVE some feedback so I can get better at this. Thank you!

#3 Updated by Marc-Andre Lafortune 8 months ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r42698.
Avdi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • enumerator.c: Allow Enumerator size argument to be any callable.
    Patch by Avdi Grimm. [bug #8641] [fix GH-362]

  • test/ruby/test_enumerator.rb: Test for above

Also available in: Atom PDF