Ticket #24 (new defect)

Opened 2 years ago

Last modified 18 months ago

YAML::load of a BigDecimal's to_yaml causes a TypeError

Reported by: awgy Owned by: somebody
Priority: major Milestone:
Component: component1 Version:
Keywords: Cc:

Description

Reproduced when following code is run in irb:

YAML::load( BigDecimal.new('1.1').to_yaml )

TypeError?: BigDecimal? can't be coerced into BigDecimal?

Change History

Changed 18 months ago by mraidel

To support BigDecimal? in the current YAML-version, just add something like the following to your code (I used "newtag" as a random tag-name). I hope there is an official solution in the next YAML-version. A description of this including a small Rails-plugin is on http://blog.induktiv.at/archives/6-YAML-and-BigDecimal-a-temporary-solution.html

class BigDecimal
  def to_yaml(opts={})
    YAML::quick_emit(object_id, opts) do |out|
      out.scalar("tag:newtag,2007:BigDecimal", self.to_s)
    end
  end
end

YAML.add_domain_type("newtag,2007", "BigDecimal") { |type, val|
  BigDecimal.new(val)
}

Note: See TracTickets for help on using tickets.