Cannot iterate over a Range of Times #4231

Closed
drcapulet opened this Issue Oct 17, 2016 · 4 comments

Projects

None yet

2 participants

@drcapulet

Environment

  • jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.92-b14 on 1.8.0_92-b14 [darwin-x86_64]
  • jruby 9.1.2.0 (2.3.0) 2016-05-26 7357c8f Java HotSpot(TM) 64-Bit Server VM 25.92-b14 on 1.8.0_92-b14 [darwin-x86_64]
  • jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 Java HotSpot(TM) 64-Bit Server VM 25.92-b14 on 1.8.0_92-b14 [darwin-x86_64]
  • Mac OS X 10.11.6

Expected Behavior

irb(main):001:0> (Time.now..(Time.now+60)) === Time.now
=> true
irb(main):002:0> (Time.now..(Time.now+60)) === Time.now - 1
=> false
irb(main):003:0> (Time.now..(Time.now+60)) === Time.now + 59
=> true
irb(main):004:0> (Time.now..(Time.now+60)) === Time.now + 61
=> false

Under MRI 2.2+

Actual Behavior

irb(main):001:0> (Time.now..(Time.now+60)) === Time.now
TypeError: can't iterate from Time
    from org/jruby/RubyRange.java:481:in `each'
    from org/jruby/RubyEnumerable.java:1225:in `include?'
    from org/jruby/RubyRange.java:710:in `include?'
    from org/jruby/RubyRange.java:715:in `==='
    from (irb):1:in `<eval>'
    from org/jruby/RubyKernel.java:995:in `eval'
    from org/jruby/RubyKernel.java:1296:in `loop'
    from org/jruby/RubyKernel.java:1115:in `catch'
    from org/jruby/RubyKernel.java:1115:in `catch'
    from /Users/alexc/.rbenv/versions/jruby-9.1.5.0/bin/irb:13:in `<main>'
@headius
Member
headius commented Oct 18, 2016

How peculiar...we have code that explicitly checks for Time and raises this error.

@headius
Member
headius commented Oct 18, 2016

So it looks like MRI does not actually trigger iteration for the == call here while JRuby does. If I actually try to iterate over a range, MRI raises the same error:

[] ~/projects/ruby $ ruby23 -e '(Time.now..(Time.now+60)) === Time.now + 61'

[] ~/projects/ruby $ ruby23 -e '(Time.now..(Time.now+60)).each {}'
-e:1:in `each': can't iterate from Time (TypeError)
    from -e:1:in `<main>'

This is telling me that what you expect may not be correct. MRI does appear to try to prevent iteration over a range of Time.

@headius
Member
headius commented Oct 18, 2016

Ok, I found the logic. MRI allows Time ranges to use cover? for include?, which avoids the iteration and error.

@headius headius closed this in a061b5c Oct 18, 2016
@headius headius added this to the JRuby 9.1.6.0 milestone Oct 18, 2016
@drcapulet

Thanks for the quick fix @headius!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment