Skip to content

Instantly share code, notes, and snippets.

@AlexanderPavlenko
Created July 25, 2013 10:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AlexanderPavlenko/6078400 to your computer and use it in GitHub Desktop.
JRuby's Enumerator#to_enum rebinds instance variables to another context?
class BuggyEnumerator < Enumerator
def initialize(that)
@ivar = that
end
def each
return to_enum(:each) unless block_given?
@ivar.each{|item| yield item }
end
end
ruby-2.0.0-p195
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3]
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
=> [1, 2, 3]
ruby-1.9.3-p327
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3]
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
=> [1, 2, 3]
jruby-1.7.4
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3]
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
NoMethodError: undefined method `each' for nil:NilClass
from (irb):7:in `each'
from org/jruby/RubyEnumerable.java:378:in `to_a'
from (irb):11:in `evaluate'
from org/jruby/RubyKernel.java:1093:in `eval'
from org/jruby/RubyKernel.java:1489:in `loop'
from org/jruby/RubyKernel.java:1254:in `catch'
from org/jruby/RubyKernel.java:1254:in `catch'
from …/.rvm/rubies/jruby-1.7.4/bin/irb:13:in `(root)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment