Today I started testing JRuby. I’m a Java a Ruby developer so I liked the mixture at first glance.
Then coding a little bit I found a difference and I wanna know if this is correct.
I’m using jruby 1.7.2 and ruby 1.8.7 for my testings.
In a jruby console I typed:
c = []
c.class #=> Array
c << 9 << 8 << 0 << 2
c.to_s #=> "[9, 8, 0, 2]"
Now in a ruby console I typed:
c = []
c.class #Array
c << 9 << 8 << 0 << 2
c.to_s #=> "9802"
What can be happening here and why result is not the same in both cases?
The difference you are seeing is not an incompatibility between Ruby and JRuby per se. JRuby 1.7.x defaults to compatibility with Ruby 1.9.3, and so what you are actually seeing is the difference in behaviour of the Array#to_s method between Ruby 1.8.7 and Ruby 1.9.3.
You can turn on JRuby’s 1.8-compatibility mode by passing the switch ‘–1.8’ to the jruby command or by setting the environment variable JRUBY_OPTS like this:
For example, this is JRuby 1.7.2’s default 1.9-compatibility mode:
And here’s the result with 1.8-compatibility turned on: