Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- john@paloalto:~/App_Academy/learn_ruby/01_temperature$ ls
- index.html temperature.rb temperature_spec.rb
- john@paloalto:~/App_Academy/learn_ruby/01_temperature$ cat temperature.rb
- def round_to(x)
- (self * 10**x).round.to_f / 10**x
- end
- def ftoc(num)
- return ((num - 32) / 1.8).round_to(1)
- end
- def ctof(num)
- return ((num *1.8) +32).round_to(1)
- end
- john@paloalto:~/App_Academy/learn_ruby/01_temperature$ cat temperature_spec.rb
- # # Topics:
- # * functions
- # * floating-point math
- #
- # # Hints
- #
- # Remember that one degree fahrenheit is 5/9 of one degree celsius, and that the freezing point of water is 0 degrees celsius but 32 degrees fahrenheit.
- #
- # In integer math, there **are no fractions**. So if you are using integer literals, you will be using integer math, which means, for example...
- #
- # 1 / 2 => 0
- #
- # In floating point math, there **are** fractions. So...
- #
- # 1.0 / 2.0 => 0.5
- #
- require "temperature"
- describe "temperature conversion functions" do
- describe "#ftoc" do
- it "converts freezing temperature" do
- ftoc(32).should == 0
- end
- it "converts boiling temperature" do
- ftoc(212).should == 100
- end
- it "converts body temperature" do
- ftoc(98.6).should == 37
- end
- it "converts arbitrary temperature" do
- ftoc(68).should == 20
- end
- end
- describe "#ctof" do
- it "converts freezing temperature" do
- ctof(0).should == 32
- end
- it "converts boiling temperature" do
- ctof(100).should == 212
- end
- it "converts arbitrary temperature" do
- ctof(20).should == 68
- end
- it "converts body temperature" do
- ctof(37).should be_within(0.1).of(98.6)
- # Why do we need to use be_within?
- # See http://www.ruby-forum.com/topic/169330
- # and http://en.wikipedia.org/wiki/IEEE_754-2008
- # and http://en.wikipedia.org/wiki/Double_precision_floating-point_format
- # Also, try "puts 0.5 - 0.4 - 0.1" -- pretty crazy, right?
- end
- end
- end
- john@paloalto:~/App_Academy/learn_ruby/01_temperature$ rake
- (in /home/john/App_Academy/learn_ruby)
- temperature conversion functions
- #ftoc
- converts freezing temperature (FAILED - 1)
- Failures:
- 1) temperature conversion functions#ftoc converts freezing temperature
- Failure/Error: ftoc(32).should == 0
- NoMethodError:
- private method `round_to' called for 0.0:Float
- # ./01_temperature/temperature.rb:6:in `ftoc'
- # ./01_temperature/temperature_spec.rb:25:in `block (3 levels) in <top (required)>'
- Finished in 0.00081 seconds
- 1 example, 1 failure
- Failed examples:
- rspec ./01_temperature/temperature_spec.rb:24 # temperature conversion functions#ftoc converts freezing temperature
- rake aborted!
- /usr/bin/ruby1.9.1 -S rspec /home/john/App_Academy/learn_ruby/01_temperature/temperature_spec.rb -I/home/john/App_Academy/learn_ruby/01_temperature -I/home/john/App_Academy/learn_ruby/01_temperature/solution -f documentation -r ./rspec_config failed
- Tasks: TOP => default => spec
- (See full trace by running task with --trace)
- john@paloalto:~/App_Academy/learn_ruby/01_temperature$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement