3. Challenge: Temperature Conversion

Now that you have learned the basic of Test-First Ruby, let's solve another challenge that includes the Test-First approach. In this lesson, you have to write code to make the test pass.

Temperature Conversion

Given a temperature in centigrade, your program should convert it to fahrenheit and vice versa. For example, given a temperatue in centigrade, 0oC, it should return 32.

Getting Started

Let's navigate to the temperature directory. In the workspace terminal, type in the following command and press enter:

cd temperature

Next, type in the following command and press enter:

bundle install

This should install a bunch of packages and dependencies.

Next, let's run the following command to run the test suites:

rspec

Since you haven't written any code yet, you will see that all the tests fail. The output should look something like this:

FFFFFFFF

Failures:

  1) #temperature #ftoc converts freezing temperature
     Failure/Error: expect(ftoc(32)).to eq(0)

     NoMethodError:
       undefined method `ftoc' for #<RSpec::ExampleGroups::Temperature::Ftoc:0x000055b85b71d108>
     # ./spec/temperature_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) #temperature #ftoc converts boiling temperature
     Failure/Error: expect(ftoc(212)).to eq(100)

     NoMethodError:
       undefined method `ftoc' for #<RSpec::ExampleGroups::Temperature::Ftoc:0x000055b85b7135b8>
     # ./spec/temperature_spec.rb:12:in `block (3 levels) in <top (required)>'

  3) #temperature #ftoc converts body temperature
     Failure/Error: expect(ftoc(98.6)).to eq(37)

     NoMethodError:
       undefined method `ftoc' for #<RSpec::ExampleGroups::Temperature::Ftoc:0x000055b85b711948>
     # ./spec/temperature_spec.rb:16:in `block (3 levels) in <top (required)>'

  4) #temperature #ftoc converts arbitrary temperature
     Failure/Error: expect(ftoc(68)).to eq(20)

     NoMethodError:
       undefined method `ftoc' for #<RSpec::ExampleGroups::Temperature::Ftoc:0x000055b85b710318>
     # ./spec/temperature_spec.rb:20:in `block (3 levels) in <top (required)>'

  5) #temperature #ctof converts freezing temperature
     Failure/Error: expect(ctof(0)).to eq(32)

     NoMethodError:
       undefined method `ctof' for #<RSpec::ExampleGroups::Temperature::Ctof:0x000055b85b7096f8>
     # ./spec/temperature_spec.rb:29:in `block (3 levels) in <top (required)>'

  6) #temperature #ctof converts boiling temperature
     Failure/Error: expect(ctof(100)).to eq(212)

     NoMethodError:
       undefined method `ctof' for #<RSpec::ExampleGroups::Temperature::Ctof:0x000055b85b709838>
     # ./spec/temperature_spec.rb:33:in `block (3 levels) in <top (required)>'

  7) #temperature #ctof converts arbitrary temperature
     Failure/Error: expect(ctof(20)).to eq(68)

     NoMethodError:
       undefined method `ctof' for #<RSpec::ExampleGroups::Temperature::Ctof:0x000055b85b702420>
     # ./spec/temperature_spec.rb:37:in `block (3 levels) in <top (required)>'

  8) #temperature #ctof converts body temperature
     Failure/Error: expect(ctof(37)).to be_within(0.1).of(98.6)

     NoMethodError:
       undefined method `ctof' for #<RSpec::ExampleGroups::Temperature::Ctof:0x000055b85b700788>
     # ./spec/temperature_spec.rb:41:in `block (3 levels) in <top (required)>'

Finished in 0.00439 seconds (files took 0.13102 seconds to load)
8 examples, 8 failures

Failed examples:

rspec ./spec/temperature_spec.rb:7 # #temperature #ftoc converts freezing temperature
rspec ./spec/temperature_spec.rb:11 # #temperature #ftoc converts boiling temperature
rspec ./spec/temperature_spec.rb:15 # #temperature #ftoc converts body temperature
rspec ./spec/temperature_spec.rb:19 # #temperature #ftoc converts arbitrary temperature
rspec ./spec/temperature_spec.rb:28 # #temperature #ctof converts freezing temperature
rspec ./spec/temperature_spec.rb:32 # #temperature #ctof converts boiling temperature
rspec ./spec/temperature_spec.rb:36 # #temperature #ctof converts arbitrary temperature
rspec ./spec/temperature_spec.rb:40 # #temperature #ctof converts body temperature

Open temperature.rb. Inside this file, you need to write a method that will convert centigrade into fahrenheit.

Your goal is to make every one of these tests pass. Once you finish writing the code, run the rspec command. Keep rewriting your code until all of the tests pass when you run the rspec command.

rspec
........

Finished in 0.00523 seconds (files took 0.12943 seconds to load)
8 examples, 0 failures

Lesson list