public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Serialize BigDecimals as Floats when using to_yaml. Closes #8746 
[ernesto.jimenez]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8877 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
NZKoz (author)
Fri Feb 15 15:33:43 -0800 2008
commit  191ffc94568ca22885eac7001cae7fef452dcfdd
tree    7f8e307d7a343fc6d3ad31896028c3e8af1f8c31
parent  3028ca59559d4c39412cdb8e4211ac6e1bded413
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Serialize BigDecimals as Floats when using to_yaml. #8746 [ernesto.jimenez]
0
+
0
 * Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing]
0
 
0
 * TimeWithZone #in_time_zone returns +self+ if zone argument is the same as #time_zone [Geoff Buesing]
...
 
 
1
2
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
0
@@ -1,6 +1,29 @@
0
+require 'yaml'
0
+
0
 class BigDecimal #:nodoc:
0
   alias :_original_to_s :to_s
0
   def to_s(format="F")
0
     _original_to_s(format)
0
   end
0
+
0
+ yaml_as "tag:yaml.org,2002:float"
0
+ def to_yaml( opts = {} )
0
+ YAML::quick_emit( nil, opts ) do |out|
0
+ # This emits the number without any scientific notation.
0
+ # I prefer it to using self.to_f.to_s, which would lose precision.
0
+ #
0
+ # Note that YAML allows that when reconsituting floats
0
+ # to native types, some precision may get lost.
0
+ # There is no full precision real YAML tag that I am aware of.
0
+ str = self.to_s
0
+ if str == "Infinity"
0
+ str = ".Inf"
0
+ elsif str == "-Infinity"
0
+ str = "-.Inf"
0
+ elsif str == "NaN"
0
+ str = ".NaN"
0
+ end
0
+ out.scalar( "tag:yaml.org,2002:float", str, :plain )
0
+ end
0
+ end
0
 end

Comments

    No one has commented yet.