Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Works as expected
$ rvm use 1.9.2
Using /Users/colszowka/.rvm/gems/ruby-1.9.2-p136
~ 1.9.2-p136 $ irb
>> "1.9".respond_to?(:encoding)
=> true
>> 7.123.round
=> 7
>> 7.123.round(2)
=> 7.12
>> exit
# Fails just as expected
$ rvm use 1.8.7
Using /Users/colszowka/.rvm/gems/ruby-1.8.7-p330
~ 1.8.7-p330 $ irb
>> "1.9".respond_to?(:encoding)
=> false
>> 7.123.round
=> 7
>> 7.123.round(2)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3:in `round'
from (irb):3
>> exit
# Sholdn't fail in 1.9 mode
$ rvm use jruby-1.6.0.RC1
Using /Users/colszowka/.rvm/gems/jruby-1.6.0.RC1
~ j1.6.0.RC1 $ ruby --1.9 -S irb
>> "1.9".respond_to?(:encoding)
=> true
>> 7.123.round
=> 7
>> 7.123.round(2)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3:in `evaluate'
from org/jruby/RubyKernel.java:1096:in `eval19'
from org/jruby/RubyKernel.java:1421:in `loop'
from org/jruby/RubyKernel.java:1208:in `rbCatch19'
from org/jruby/RubyKernel.java:1208:in `rbCatch19'
>> exit
@colszowka
Copy link
Author

The problem can be temporarily fixed with this code from ActiveSupport's rounding:
class Float
alias_method :precisionless_round, :round
def round(precision = nil)
if precision
magnitude = 10.0 ** precision
(self * magnitude).round / magnitude
else
precisionless_round
end
end
end

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