Today, I would like to get pretty printing of
Float
instances working. Specifically I would like:1.125.to_s == '1⅛'
1.25.to_s == '1¼'
0.875.to_s == '⅞'
describe Float, "pretty printing" doAs expected, that example passes without any changes. Next up, I describe precision. There is no reason to specify more than two decimal points when trying to measure things, so, in RSpec format:
specify "1.23.to_s == '1.23'" do
1.23.to_s.should == "1.23"
end
specify "1.236.to_s == '1.24'" doThat fails with:
1.236.to_s.should == "1.24"
end
1)To get that to pass, I re-open the
'Float to_s should only include 2 decimals precision' FAILED
expected: "1.24",
got: "1.236" (using ==)
./spec/float_spec.rb:8:
Float
to override to_s
:class FloatThe updated
def to_s
"%.2f" % self
end
end
Float
class is not automatically included in my Sinatra application (and thus in the RSpec examples). I save it in the lib
directory, and then add this to the Sinatra application:$: << File.expand_path(File.dirname(__FILE__) + '/lib')For the remaining numbers that are likely to appear in recipes using imperial units of measure, I use these examples:
require 'float'
specify "0.25.to_s == '¼'"I end up using a long
specify "0.5.to_s == '½'"
specify "0.75.to_s == '¾'"
specify "0.33.to_s == '⅓'"
specify "0.66.to_s == '⅔'"
specify "0.125.to_s == '⅛'"
specify "0.325.to_s == '⅜'"
specify "0.625.to_s == '⅝'"
specify "0.875.to_s == '⅞'"
case
statement to get that passing:class FloatIn addition to pretty printing things that look like fractions, I also want to print things that look like integers:
def to_s
int = self.to_i
frac = pretty_fraction(self - int)
if frac
(int == 0 ? "" : int.to_s) + frac
else
"%.2f" % self
end
end
private
def pretty_fraction(fraction)
case fraction
when 0.25
"¼"
when 0.5
"½"
when 0.75
"¾"
when 0.33
"⅓"
when 0.66
"⅔"
when 0.125
"⅛"
when 0.325
"⅜"
when 0.625
"⅝"
when 0.875
"⅞"
end
end
end
specify "1.0.to_s == '1'" doI get that to pass with another condition on the
1.0.to_s.should == "1"
end
case
statement:case fractionLast up is a need to account for the cases when the author goes overboard with precision. That is, what happens if I entered 0.67 or 0.667 to mean two-thirds. In RSpec:
...
when 0
""
end
specify "0.33.to_s == '⅓'" doRather than account for all possible cases, I use a range in my case statement:
0.33.to_s.should == "⅓"
end
specify "0.333.to_s == '⅓'" do
0.333.to_s.should == "⅓"
end
specify "0.66.to_s == '⅔'" do
0.66.to_s.should == "⅔"
end
specify "0.667.to_s == '⅔'" do
0.667.to_s.should == "⅔"
end
def pretty_fraction(fraction)This makes use of
case fraction
...
when 0.33..0.34
"⅓"
when 0.66..0.67
"⅔"
...
end
end
case
statements performing a triple equality operation on the target and that:>> (0.66..0.67) === 0.666With my floats printing nicely, I have cleared up the most bothersome of the minor issues that I found on the beta site. Tomorrow, I will work through a few more issues and redeploy.
=> true
No comments:
Post a Comment