Friday, January 2, 2009

Ruby

This is a mini update. I plan on making a longer update later today, but I wanted to share my first practical Ruby code.

A little back story: I have always been interested in Computer Programming and signed up for a free class online at Ruby Learning. I have learned a lot about how programming works and had a real reality check at what it takes to create a quality program. It's not shiny and beautiful from the start; It's numbers and logic.

Well, here's my basic ruby code, flawed yet effective. It converts temperature from Fahrenheit to Celsius.

# temperature converter

def convert_f(temp_f)
(temp_f.to_f - 32) * 5/9
end

puts "What\'s the temperature in Fahrenheit?\n(eg. 54)"
STDOUT.flush
temp_f = gets.chomp

temp_c = convert_f(temp_f)
puts 'That\'s ' + format("%.1f", temp_c) + ' degrees Celcius.'

if temp_c <= 0
puts 'It\'s freezing!'
end
if temp_c > 0 and temp_c <= 10
puts 'It\'s cold!'
end
if temp_c > 10 and temp_c <= 15
puts 'It\'s a little chilly.'
end
if temp_c > 15 and temp_c <= 30
puts 'It\'s nice.'
end
if temp_c > 30
puts 'It\'s roasting!'
end

No comments:

Post a Comment