Ruby: Fahrenheit to Celsius

The best way for me to learn a programming language is practicing writing programs and editing written programs. That’s how I’ve come to understand JavaScript better. And now I’m taking this approach while learning Ruby and Python.

While reading through Ruby for Rails by David A. Black, I had the chance to practice and work ahead on a small but applicable program that converts Fahrenheit to Celsius. Well, the book actually does it the other way around, but I like to tweak programs my way to test my own understanding of how it functions.

It was a quick program to write in < 30 minutes and a nice refresher on F & C formulas:

# ask for a number in Fahrenheit
puts 'Convert Fahrenheit to Celsius. Please enter a Fahrenheit temperature:'
# get a number from the user
f = gets.chomp
# apply the conversion formula from F to C
c = (f.to_i - 32) * 5 / 9
# output the formula in string to the user
puts f.to_s + 'F is ' + c.to_s + 'C.'

This program is a nod to my older brother who lives in Taiwan and constantly tells me his location’s temperature in Celsius. It should be noted that he is also the one who first encouraged me to learn Ruby. Thanks, Tim!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s