2012-05-23 77 views
6

這應該是非常簡單的,但它是炸燬。有任何想法嗎?BigDecimal不能被強制爲BigDecimal

d = BigDecimal.new("2.0") 
YAML::load({:a => d}.to_yaml) 
TypeError: BigDecimal can't be coerced into BigDecimal 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `inspect' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `inspect' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `block in <module:IRB>' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:30:in `call' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:30:in `inspect_value' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/context.rb:260:in `inspect_last_value' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:311:in `output_value' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:160:in `block (2 levels) in eval_input' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:273:in `signal_status' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:155:in `eval_input' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:70:in `block in start' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:69:in `catch' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:69:in `start' 
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'Maybe IRB bug! 
+0

嗯,看起來像'to_yaml'對BigDecimal,'BigDecimal.new(「2.0」),to_yaml'說''---!!ruby/object:BigDecimal {} \ n \ n「'感到困惑。 –

+1

是的。 'ActiveSupport'似乎爲此添加了一個核心擴展,但在我的平臺上,我得到了'BigDecimal.new(「2.0」).to_yaml' - >'「--- 2.0 \ n」'。因此,儘管它效果更好,但它仍不會轉換爲完整的BigDecimal:http://apidock.com/rails/BigDecimal/to_yaml – Casper

回答

1

這是一個已經reportedfixed的錯誤。

最好的解決方案是升級到最新的Ruby(修補程序在修補程序級別194以上)。

如果你不能升級你的Ruby版本,你可以通過安裝Psych gem來獲得修復。如果你這樣做,你需要在require 'yaml'之前添加gem 'psych'(或者如果你使用Bundler,將它添加到你的Gemfile中)從gem加載代碼而不是從標準庫加載代碼。

0

是的,我碰到過那一次。這裏是什麼,我做了一個版本:

YAML & BigDecimal workaround

+0

不幸的是,該解決方案不再有效。我使用紅寶石1.9.3 –

0

這裏是大衛的回答,更新1.9.3由於工作this related question

require 'yaml' 
require 'bigdecimal' 
YAML::ENGINE.yamler= 'syck' 
class BigDecimal 
    def to_yaml(opts={}) 
    YAML::quick_emit(object_id, opts) do |out| 
     out.scalar("tag:induktiv.at,2007:BigDecimal", self.to_s) 
    end 
    end 
end 
YAML.add_domain_type("induktiv.at,2007", "BigDecimal") do |type, val| 
    BigDecimal.new(val) 
end 
x = BigDecimal.new("2.0") 
puts x.to_yaml 
y = YAML.load(x.to_yaml) 
puts x == y