This week I'm highlighting the build of a simple Kelvin to Fahrenheit converter in four different programming languages: JavaScript, Python, Ruby and PHP

Today's approach: Ruby

In this mini-project we are going to create a mini application in the console that converts the temperature from degrees kelvin to degrees fahrenheit. If you don't understand the difference between fahrenheit and kelvin that is ok. I'll share with you the formula as we walk through the steps.

I will list out the 6 steps below that you need to take to build out this mini-application. In the following video I will walk through all 6 steps myself so you can see a possible solution.

If you are looking for a coding environment you can go to repl.it and use their platform for free.

Challenge: Convert any given temperature in Kelvin to Fahrenheit

  1. Let's imagine that the weather reports says that the temperature today will be 301 Kelvin. How should you dress for the day? Let's create a program that lets us know the temperature in fahrenheit. To start, create a variable named kelvin_temp, and set it equal to 301. Write a comment above this line of code that explains this line of code.
  2. Finding the temperature in Celsius is straightforward when you know the temperature in Kelvin — the only difference is that Celsius is 273.15 degrees less than Kelvin.
    Let's convert Kelvin to Celsius by subtracting 273.15 from the kelvin_temp variable. Store the result in another variable, named celsius_temp.
    Write a comment above that explains this line of code.
  3. Use the equation below to calculate the temperature in Fahrenheit, then store the answer in a variable named fahrenheit_temp.

Fahrenheit = Celsius * 9 / 5 + 32

Write a comment above that explains this line of code.

4. print to the console the value of fahrenheit_temp. It should begin with 82.13.

In our next step we are going to see what we can do to make sure that our number is a whole number by rounding down.

5. As we have just seen, when you convert from Celsius to Fahrenheit, you often get a decimal number. Go ahead and delete the print command from step 4.

Let's use the floor() method to round down the Fahrenheit temperature. Save the result to the fahrenheit_temp variable. Write a comment above that explains this line of code.

6. Use print and string concatenation to display the temperature in fahrenheit_temp to the terminal to create the message as follows:

The temperature is [TEMPERATURE] degrees Fahrenheit.

TEMPERATURE should be determined by the value of fahrenheit_temp.

Great job! I'll walk through these steps in the next video!

Image result for coding gif

Finished Code:

# Temperature in kelvin stored in variable `kelvin_temp`
kelvin_temp = 301
# temperature in celsius stored in variable `celsius_temp`
celsius_temp = kelvin_temp - 273.15
# convert celsius to fahrenheit stored in variable `fahrenheit_temp`
fahrenheit_temp = celsius_temp * (9/5) + 32
# round the value of fahrenheit down and assign to `fahrenheit_temp`
fahrenheit_temp = fahrenheit_temp.floor;
# use string concatenation to leave a message
# "The temperature is `fahrenheit` degrees Fahrenheit."
print "The temperature is #{fahrenheit_temp} degrees Fahrenheit."