Lelon
Lelon

Reputation: 905

JRuby 1.6.4 from source: undefined method `to_i' for Rational

irb(main):001:0> test = (0/1).rationalize
=> (0/1)
irb(main):002:0> test.to_i
NoMethodError: undefined method `to_i' for (0/1):Rational
        from (irb):2:in `evaluate'
        from org/jruby/RubyKernel.java:1093:in `eval'
        from org/jruby/RubyKernel.java:1419:in `loop'
        from org/jruby/RubyKernel.java:1205:in `catch'
        from org/jruby/RubyKernel.java:1205:in `catch'
        from C:\Development\jruby-1.6.4\bin\irb:13:in `(root)'
irb(main):003:0>

This works in JRuby 1.6.4 when installed normally, but after having built from source I get the error.

Upvotes: 0

Views: 337

Answers (2)

Lelon
Lelon

Reputation: 905

This is a bug in JRuby.

https://jira.codehaus.org/browse/JRUBY-6142

The ticket includes a fix but it has not been merged.

If like me, you were only encountering this problem when installing gems, try making sure you aren't running with the --debug flag. If you're having this problem using a JRuby installation you built from source, try the precompiled version from the website.

Upvotes: 0

banzaiman
banzaiman

Reputation: 2663

This should not work in the 1.8 mode.

$ ruby -e 'p (0/1).rationalize.to_i'
-e:1: undefined method `rationalize' for 0:Fixnum (NoMethodError)
$ jruby -e 'p (0/1).rationalize.to_i'
NoMethodError: undefined method `rationalize' for 0:Fixnum
  (root) at -e:1

In the 1.9 mode, on the other hand, it should work as expected:

$ ruby1.9 -e 'p (0/1).rationalize.to_i'
0
$ jruby --1.9 -e 'p (0/1).rationalize.to_i'
0

Upvotes: 1

Related Questions