Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8746 (closed defect: fixed)

Opened 1 year ago

Last modified 6 months ago

[PATCH] [TINY] Add to_yaml support for BigDecimal

Reported by: vesaria Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: yaml BigDecimal verified tiny patch
Cc:

Description

BigDecimal currently does not support to_yaml. This is especially irksome, as ActiveRecord now uses BigDecimal for NUMERIC, breaking the useful to_yaml method, dumping to fixtures, y in the console, etc.

The attached is a tiny patch to fix BigDecimal#to_yaml. Also included are unit tests.

The patch has been running in an application for several days without any reported problem.

Attachments

big_decimal_yaml_fix.rb (1.3 kB) - added by vesaria on 06/25/07 18:40:36.
Simple patch and unit tests
bigdecimal.diff (2.1 kB) - added by shift on 06/25/07 20:36:15.
bigdecimal patch
bigdecimal2.diff (2.1 kB) - added by vesaria on 10/10/07 18:22:55.
Fixes spelling error in patch comment
bigdecimal_to_yaml_in_correct_path.diff.txt (1.7 kB) - added by ernesto.jimenez on 02/13/08 01:06:21.
forgot to add the yaml_as
bigdecimal_to_yaml_in_correct_path.diff (1.8 kB) - added by ernesto.jimenez on 02/13/08 01:08:03.
Oops, uploaded unmodified patch xD

Change History

06/25/07 18:40:36 changed by vesaria

  • attachment big_decimal_yaml_fix.rb added.

Simple patch and unit tests

06/25/07 18:41:30 changed by vesaria

  • keywords set to tiny patch yaml BigDecimal.

06/25/07 18:53:56 changed by bitsweat

  • status changed from new to closed.
  • resolution set to incomplete.
  • component changed from ActiveRecord to ActiveSupport.

This belongs in Active Support's core extensions.

06/25/07 20:36:15 changed by shift

  • attachment bigdecimal.diff added.

bigdecimal patch

06/26/07 01:48:38 changed by vesaria

  • status changed from closed to reopened.
  • resolution deleted.

Thanks shift for the fix. I think this may be complete now - is anything lacking?

06/29/07 02:01:46 changed by vesaria

Please let me know if anything is lacking.

07/12/07 22:29:58 changed by vesaria

Core: if this is good, please close it. If it is incomplete, please let me know what's missing.

07/24/07 18:12:56 changed by kampers

  • status changed from reopened to closed.
  • resolution set to incomplete.

This still belongs in core extensions, not the Active Support root.

Also, spelling error in docs: loose -> lose.

07/24/07 18:13:32 changed by kampers

  • keywords changed from tiny patch yaml BigDecimal to yaml BigDecimal.
  • summary changed from [PATCH] [TINY] Add to_yaml support for BigDecimal to [PATCH] Add to_yaml support for BigDecimal.

10/10/07 18:21:59 changed by vesaria

  • status changed from closed to reopened.
  • resolution deleted.

This still belongs in core extensions, not the Active Support root.

- Fixed by shift (thanks)

Also, spelling error in docs: loose -> lose.

- Fixed in accompanying attachment

Anything else needed for this patch?

10/10/07 18:22:55 changed by vesaria

  • attachment bigdecimal2.diff added.

Fixes spelling error in patch comment

10/10/07 18:23:22 changed by vesaria

  • summary changed from [PATCH] Add to_yaml support for BigDecimal to [PATCH] [TINY] Add to_yaml support for BigDecimal.

11/26/07 22:46:08 changed by vesaria

  • keywords changed from yaml BigDecimal to yaml BigDecimal unverified tiny patch.

12/14/07 00:45:37 changed by mdemare

+1

12/15/07 00:07:58 changed by david

This should follow the same pattern as all other extensions in core_ext. Like using lib/core_ext/big_decimal/conversions.rb and a lib/core_ext/bigdecimal.rb

02/11/08 23:25:46 changed by ernesto.jimenez

I have submited the patch placing the code in the correct place: method to_yaml in lib/core_ext/big_decimal/conversions.rb tests in test/core_ext/bigdecimal.rb

02/12/08 16:48:39 changed by juanjo.bazan

+1

02/13/08 01:06:21 changed by ernesto.jimenez

  • attachment bigdecimal_to_yaml_in_correct_path.diff.txt added.

forgot to add the yaml_as

02/13/08 01:08:03 changed by ernesto.jimenez

  • attachment bigdecimal_to_yaml_in_correct_path.diff added.

Oops, uploaded unmodified patch xD

02/13/08 11:27:19 changed by david.calavera

  • keywords changed from yaml BigDecimal unverified tiny patch to yaml BigDecimal verified tiny patch.

+1

02/15/08 23:33:47 changed by nzkoz

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [8877]) Serialize BigDecimals as Floats when using to_yaml. Closes #8746 [ernesto.jimenez]

02/16/08 00:06:16 changed by bitsweat

It's worth noting this silently loses precision.

02/26/08 12:57:19 changed by mike

This patch deserializes a BigDecimal as float. If this is not what you want, you should define a new custom format for YAML. I have done this for myself: http://blog.induktiv.at/archives/6-YAML-and-BigDecimal-a-temporary-solution.html

It uses a custom tag and so doesn't qualify for inclusion in the Rails-core, but it works very well for me and deserializes as BigDecimal. If there is interest, I would also turn it into a patch (there is already a plugin with some very basic unit tests).