Programming Fundamentals/Functions/Ruby
functions.rb
edit# This program asks the user for a Fahrenheit temperature,
# converts the given temperature to Celsius,
# and displays the results.
#
# References:
# https://www.mathsisfun.com/temperature-conversion.html
# https://en.wikibooks.org/wiki/Ruby_Programming
def get_fahrenheit()
puts "Enter Fahrenheit temperature:"
fahrenheit = gets.chomp.to_f
return fahrenheit
end
def calculate_celsius(fahrenheit)
celsius = (fahrenheit - 32) * 5 / 9
return celsius
end
def display_result(fahrenheit, celsius)
puts (fahrenheit).to_s + "° Fahrenheit is " + (celsius).to_s + "° Celsius"
end
def main()
fahrenheit = get_fahrenheit()
celsius = calculate_celsius(fahrenheit)
display_result(fahrenheit, celsius)
end
main()
Try It
editCopy and paste the code above into one of the following free online development environments or use your own Ruby compiler / interpreter / IDE.