Advertisement
Guest User

Untitled

a guest
Nov 7th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.11 KB | None | 0 0
  1. john@paloalto:~/App_Academy/learn_ruby/01_temperature$ ls
  2. index.html  temperature.rb  temperature_spec.rb
  3. john@paloalto:~/App_Academy/learn_ruby/01_temperature$ cat temperature.rb
  4. def round_to(x)
  5.   (self * 10**x).round.to_f / 10**x
  6. end
  7.  
  8. def ftoc(num)
  9.   return ((num - 32) / 1.8).round_to(1)
  10. end
  11.  
  12. def ctof(num)
  13.   return ((num *1.8) +32).round_to(1)
  14. end
  15. john@paloalto:~/App_Academy/learn_ruby/01_temperature$ cat temperature_spec.rb
  16. # # Topics:
  17. # * functions
  18. # * floating-point math
  19. #
  20. # # Hints
  21. #
  22. # 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.
  23. #
  24. # In integer math, there **are no fractions**. So if you are using integer literals, you will be using integer math, which means, for example...
  25. #
  26. #    1 / 2 => 0
  27. #
  28. # In floating point math, there **are** fractions. So...
  29. #
  30. #    1.0 / 2.0 => 0.5
  31. #
  32.  
  33. require "temperature"
  34.  
  35. describe "temperature conversion functions" do
  36.  
  37.   describe "#ftoc" do
  38.  
  39.     it "converts freezing temperature" do
  40.       ftoc(32).should == 0
  41.     end
  42.  
  43.     it "converts boiling temperature" do
  44.       ftoc(212).should == 100
  45.     end
  46.  
  47.     it "converts body temperature" do
  48.       ftoc(98.6).should == 37
  49.     end
  50.  
  51.     it "converts arbitrary temperature" do
  52.       ftoc(68).should == 20
  53.     end
  54.  
  55.   end
  56.  
  57.   describe "#ctof" do
  58.  
  59.     it "converts freezing temperature" do
  60.       ctof(0).should == 32
  61.     end
  62.  
  63.     it "converts boiling temperature" do
  64.       ctof(100).should == 212
  65.     end
  66.  
  67.     it "converts arbitrary temperature" do
  68.       ctof(20).should == 68
  69.     end
  70.  
  71.     it "converts body temperature" do
  72.       ctof(37).should be_within(0.1).of(98.6)
  73.       # Why do we need to use be_within?
  74.       # See http://www.ruby-forum.com/topic/169330
  75.       # and http://en.wikipedia.org/wiki/IEEE_754-2008
  76.       # and http://en.wikipedia.org/wiki/Double_precision_floating-point_format
  77.       # Also, try "puts 0.5 - 0.4 - 0.1" -- pretty crazy, right?
  78.     end
  79.  
  80.   end
  81.  
  82. end
  83. john@paloalto:~/App_Academy/learn_ruby/01_temperature$ rake
  84. (in /home/john/App_Academy/learn_ruby)
  85.  
  86. temperature conversion functions
  87.   #ftoc
  88.     converts freezing temperature (FAILED - 1)
  89.  
  90. Failures:
  91.  
  92.   1) temperature conversion functions#ftoc converts freezing temperature
  93.      Failure/Error: ftoc(32).should == 0
  94.      NoMethodError:
  95.        private method `round_to' called for 0.0:Float
  96.     # ./01_temperature/temperature.rb:6:in `ftoc'
  97.     # ./01_temperature/temperature_spec.rb:25:in `block (3 levels) in <top (required)>'
  98.  
  99. Finished in 0.00081 seconds
  100. 1 example, 1 failure
  101.  
  102. Failed examples:
  103.  
  104. rspec ./01_temperature/temperature_spec.rb:24 # temperature conversion functions#ftoc converts freezing temperature
  105. rake aborted!
  106. /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
  107.  
  108. Tasks: TOP => default => spec
  109. (See full trace by running task with --trace)
  110. john@paloalto:~/App_Academy/learn_ruby/01_temperature$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement