beatrichartz / exchange

Easy Currency Operations directly available on your numbers - maintenance discontinued
http://beatrichartz.github.com/exchange
MIT License
42 stars 22 forks source link

Error getting precision when trying to parse Rationals #4

Closed ryanza closed 11 years ago

ryanza commented 11 years ago

Heya!

We're using your gem extensively through our application; what we do is store all our monetary values as micro cents then convert back later. The issue is that some of our values come back as Rationals which doesn't get parsed by the following line:

https://github.com/beatrichartz/exchange/blob/master/lib/exchange/iso.rb#L192

When the value is nil it breaks there too, which probably makes sense to include some type of sanity check before just trying to regex major and minor precision. :)

1.9.3p374 :005 > 6.0e-05.in(:eur)
NoMethodError: undefined method `map' for nil:NilClass
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/exchange-1.1.0/lib/exchange/iso.rb:192:in `precision_for'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/exchange-1.1.0/lib/exchange/iso.rb:94:in `instantiate'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/exchange-1.1.0/lib/exchange/money.rb:61:in `initialize'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/exchange-1.1.0/lib/exchange/core_extensions/numeric/conversability.rb:33:in `new'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/exchange-1.1.0/lib/exchange/core_extensions/numeric/conversability.rb:33:in `in'
    from (irb):5
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/ryan/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
1.9.3p374 :006 > 
beatrichartz commented 11 years ago

Hi!

Nice to hear, hope it is useful!

I added instantiation of floats in scientific notation / rationals.

New release is 1.1.1, be sure to install it :)

ryanza commented 11 years ago

Awesome! Bumping our version now on all our apps. (Yes we have a few) :+1: