JRuby 1.4: possible Set#each_with_index or Enumerator issue

I’m trying to have Set#pairs (as implemented in the attachment) to work
under JRuby (both --1.8 and --1.9), but fail miserably. I’m using the
backports gem¹ to get 1.9’s Enumerator in 1.8.

¹ http://github.com/marcandre/backports

MRI:

[email protected]:~$ ruby -v pairs.rb
ruby 1.8.7 (2009-09-11 patchlevel 202) [x86_64-linux]
Array#pairs
true
Set#pairs
true
[email protected]:~$ ruby -v pairs.rb
ruby 1.9.1p243 (2009-07-16) [x86_64-linux]
Array#pairs
true
Set#pairs
true

JRuby:

[email protected]:~$ jruby --1.8 -v pairs.rb
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java
HotSpot™ 64-Bit Server VM 1.6.0_16) [amd64-java]
Array#pairs
true
Set#pairs
false
[email protected]:~$ jruby --1.9 -v pairs.rb
jruby 1.4.0 (ruby 1.9.2dev trunk 24787) (2009-11-02 69fbfa3) (Java
HotSpot™ 64-Bit Server VM 1.6.0_16) [amd64-java]
Array#pairs
true
pairs.rb:9:in `pairs’: wrong # of arguments(0 for 1) (ArgumentError)
from pairs.rb:20

(Apparently, Array#combination works, so as a workaround I can use
Set[1,2,3,4].to_a.combination(2), but I’d rather not create Arrays
every time I want to get Sets’ elements in pairs.)

— Shot

the problem is that Enumerator class in jruby doesn’t accept a block as
initialization parameter yet. There is a filed issue about this problem.
There were some list threads talking about move the Enumerator
implementation to ruby so I don’t know where this bug will be fixed:

http://jira.codehaus.org/browse/JRUBY-4098

David C.:

the problem is that Enumerator class in jruby doesn’t accept a block
as initialization parameter yet. There is a filed issue about this
problem.

Thanks, this makes sense.

I took a look at the backports gem’s lib/backports/1.9/enumerator.rb¹
and it seems it tries to cater to JRUBY-3609, but I assume it’s just so
the gem doesn’t break (rather than provide block-based Enumerator.new
support).

¹
http://github.com/marcandre/backports/blob/master/lib/backports/1.9/enumerator.rb

There were some list threads talking about move the Enumerator
implementation to ruby so I don’t know where this bug will be fixed

Maybe the backports gem’s Yielder class² would be a good starting point?
(Every time I think I’m finally starting to grasp the problem with
implementing Enumerator in Java I get quickly corrected by seeing
a simple example that ‘hmmm, looks sane – but if it was this simple
it’d be implemented like this a long time ago’…)

²
http://github.com/marcandre/backports/blob/master/lib/backports/tools.rb

— Shot