ylluminate
ylluminate

Reputation: 12369

"can't convert BigDecimal into String" on Ruby 1.9.2p290, but not on 1.9.3p0

I have a scenario with code like the following:

extracted_data = data.map{|row| ((row.some_long_number.to_f) if BigDecimal(row.some_long_number) != BigDecimal("-1500.0")) }.reverse

When I run this on Rails 3.1.x with ruby 1.9.3 it works out fine, however when I shift this over to Heroku Cedar with the same Rails version but ruby 1.9.2, I end up with the following exception:

can't convert BigDecimal into String

app/controllers/some_controller.rb:12:in `BigDecimal'
app/controllers/some_controller.rb:12:in `block (2 levels) in index'
app/controllers/some_controller.rb:12:in `map'
app/controllers/some_controller.rb:12:in `block in index'
app/controllers/some_controller.rb:3:in `new'
app/controllers/some_controller.rb:3:in `index'

What could be going on here from ruby 1.9.2 to 1.9.3 that sets this off?

Upvotes: 1

Views: 1765

Answers (1)

Michael Papile
Michael Papile

Reputation: 6856

From Ruby changelog:

  • bigdecimal/bigdecimal.c (BigDecimal_new): support generating a new BigDecimal from another BigDecimal using BigDecimal global function or constructor. [ruby-dev:44245]

I suspect that BigDecimal(row.some_long_number) is making a BigDecimal of something that is already a BigDecimal. The constructor takes a String, so it is trying to coerce the BigDecimal to a string. The change in 1.9.3 allows for this, but 1.9.2 doesn't.

So you can do something like:

row.some_long_number.respond_to?(:to_s) ? BigDecimal(row.some_long_number) : row.some_long_number

Upvotes: 2

Related Questions