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.
今天我开始测试JRuby。我是一名Java开发人员,所以我很喜欢这种混合物。然后编码一点我发现了差异,我想知道这是否正确。我正在使用jruby 1.7.2和ruby 1.8.7进行测试。
In a jruby console I typed:
在一个jruby控制台我输入:
c = []
c.class #=> Array
c << 9 << 8 << 0 << 2
c.to_s #=> "[9, 8, 0, 2]"
Now in a ruby console I typed:
现在在我输入的ruby控制台中输入:
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?
这里可以发生什么,为什么两种情况下结果都不一样?
1 个解决方案
#1
5
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.
您看到的差异不是Ruby和JRuby本身之间的不兼容性。 JRuby 1.7.x默认与Ruby 1.9.3兼容,因此您实际看到的是Ruby 1.8.7和Ruby 1.9.3之间的Array#to_s方法的行为差异。
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:
您可以通过将开关'--1.8'传递给jruby命令或通过设置环境变量JRUBY_OPTS来打开JRuby的1.8兼容模式:
export JRUBY_OPTS='--1.8'.
For example, this is JRuby 1.7.2's default 1.9-compatibility mode:
例如,这是JRuby 1.7.2的默认1.9兼容模式:
$ JRUBY_OPTS='' irb
> ([] << 9 << 8 << 0 << 2).to_s
=> "[9, 8, 0, 2]"
And here's the result with 1.8-compatibility turned on:
这是打开1.8兼容性的结果:
$ JRUBY_OPTS='--1.8' irb
> ([] << 9 << 8 << 0 << 2).to_s
=> "9802"
#1
5
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.
您看到的差异不是Ruby和JRuby本身之间的不兼容性。 JRuby 1.7.x默认与Ruby 1.9.3兼容,因此您实际看到的是Ruby 1.8.7和Ruby 1.9.3之间的Array#to_s方法的行为差异。
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:
您可以通过将开关'--1.8'传递给jruby命令或通过设置环境变量JRUBY_OPTS来打开JRuby的1.8兼容模式:
export JRUBY_OPTS='--1.8'.
For example, this is JRuby 1.7.2's default 1.9-compatibility mode:
例如,这是JRuby 1.7.2的默认1.9兼容模式:
$ JRUBY_OPTS='' irb
> ([] << 9 << 8 << 0 << 2).to_s
=> "[9, 8, 0, 2]"
And here's the result with 1.8-compatibility turned on:
这是打开1.8兼容性的结果:
$ JRUBY_OPTS='--1.8' irb
> ([] << 9 << 8 << 0 << 2).to_s
=> "9802"