From bd032ca03bfeb813c9be42bc26b7096d9e1d8657 Mon Sep 17 00:00:00 2001 From: Evan Senter Date: Tue, 26 Jun 2012 12:05:26 -0400 Subject: [PATCH] Patching awesome_bigdecimal to not have lossy precision. --- Gemfile.lock | 2 +- lib/awesome_print/formatter.rb | 11 ++++++++--- lib/awesome_print/inspector.rb | 1 + spec/formats_spec.rb | 12 ++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e097959..8acbb80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - awesome_print (1.0.0) + awesome_print (1.0.2) GEM remote: http://rubygems.org/ diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index a639168..0ef16cc 100755 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -189,12 +189,17 @@ module AwesomePrint colorize(ls.empty? ? d.inspect : "#{d.inspect}\n#{ls.chop}", :dir) end - # Format BigDecimal and Rational objects by convering them to Float. + # Format BigDecimal object. #------------------------------------------------------------------------------ def awesome_bigdecimal(n) - colorize(n.to_f.inspect, :bigdecimal) + colorize(n.to_s("F"), :bigdecimal) + end + + # Format Rational object. + #------------------------------------------------------------------------------ + def awesome_rational(n) + colorize(n.to_s, :rational) end - alias :awesome_rational :awesome_bigdecimal # Format a method. #------------------------------------------------------------------------------ diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index b0ead7c..e048f69 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -43,6 +43,7 @@ module AwesomePrint :keyword => :cyan, :method => :purpleish, :nilclass => :red, + :rational => :blue, :string => :yellowish, :struct => :pale, :symbol => :cyanish, diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index 261ba45..3d0ae5f 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -479,14 +479,14 @@ EOS #------------------------------------------------------------------------------ describe "BigDecimal and Rational" do - it "should present BigDecimal object as Float scalar" do - big = BigDecimal("2010.4") - big.ai(:plain => true).should == "2010.4" + it "should present BigDecimal object with arbitrary precision" do + big = BigDecimal("201020102010201020102010201020102010.4") + big.ai(:plain => true).should == "201020102010201020102010201020102010.4" end - it "should present Rational object as Float scalar" do - rat = Rational(2010, 2) - rat.ai(:plain => true).should == "1005.0" + it "should present Rational object with arbitrary precision" do + rat = Rational(201020102010201020102010201020102010, 2) + rat.ai(:plain => true).should == "100510051005100510051005100510051005/1" end end