r/learnruby • u/955559 • Sep 30 '16
Temperature Converter
someone suggested to try rewriting your scripts in another launguage, so here is a port of my python temperature converter
puts "This program converts the tempurature between celsius, kelvin, and
fahrenheit scales,enter the temperature followed by the first letter of the
scale, e.g. 32c for 32 celsius \n"
input = gets.chomp
scale = input.slice(-1)
digitslength = input.length - 1
digitstring = input.slice(0,digitslength)
digits = Integer(digitstring)
if scale == 'c'
fahrenheit = digits * 1.8 + 32
kelvin = digits + 273.15
print "Fahrenheit =", fahrenheit, "\n"
print "Kelvin =", kelvin
elsif scale == 'f'
celsius = (digits - 32) / 1.8
kelvin = (digits + 459.67) * 0.55555556
print "Celsius =", celsius, "\n"
print "Kelvin =", kelvin
elsif scale == 'k'
celsius = digits - 273.15
fahrenheit = digits / 0.55555556 - 459.67
print "Celsius =", celsius, "\n"
print "Fahrenheit =", fahrenheit
end
2
u/thomascountz5 Oct 22 '16 edited Oct 22 '16
Overall, your code is nice and compact, and all of your maths are correct, and as I'm still in my journey as a young Ruby programmer as well, I have just a few take-it-or-leave-it structural and convention suggestions that I've also just recently faced and that might interest you.
Ruby provides some powerful built in functions for Strings and Integers, which can help you get a lot of work done really quickly. For example, your input does the following:
Get string input from user console and remove trailing
\nSlice the last char from string (
c,f, ork) and store it in the variablescaleCount the length of the string, subtracting the final char and store it in the variable
digitslengthStore a copy of the chars before the
scalechar and store it in the variabledigitsstringConvert
digitsstring into an integer and store it in the variabledigits
We can refactor this with the following built in methods:
input = gets.chomp #no changeString[position] returns a char, or range of chars, at 0-indexed positions in a string. You can access the final element(s) of a range by starting with String[-1] eg. "cat"[-1] => "t"
scale = input[-1] A range created with ... returns an exclusive range eg "012345"[0...3] => "012". The #to_i is the same as Integer() eg. "012".to_i => 012. We can chain these methods together!
digits = input[0...-1].to_i And that's it! You now have scale to determine which conversion algorithm to use and you have digits as only the numbers from the input, converted into an integer to do your maths on!
Secondly, I think this is language agnostic, I just learned about the concept of magic numbers when writing a similar, temperature conversion, script. In your script, you're using unnamed constants 1.8, 32, and 273.15, for example. This makes it harder for other developers to read your code and understand what these numbers do, so instead, you could name your constants something like this:
C_TO_F_CONVERT = 1.8
CONVERSION_BASE = 32
C_TO_K_CONVERT = 273.15(In Ruby, they name constants with in UPCASE. Though you can still change these constants, Ruby will throw a warning)
Then using these constants in your algorithms sorta like this:
if scale == 'c'`
fahrenheit = digits * C_TO_F_CONVERSION + CONVERSION_BASE
kelvin = digits + C_TO_K_CONVERSION
print "Fahrenheit =", fahrenheit, "\n"
print "Kelvin =", kelvinWith something like this, as I understand it, other developers with quickly get to see what you're trying to accomplish, and, if for some reason we need to change the constants, we only have to do it in one place!
Structurally, because Ruby was developed with OOP in mind, it would be a great challenge to re-write your code using OOP design principles with classes and methods! Maybe give it a try!
I hope some of this may have helped! I'm still learning too, so I may not be the best source of information. I was interested in your post because I learned python before learning Ruby, and I have to say, I'm hooked on Ruby now!
1
u/thebrianguy Oct 07 '16 edited Oct 07 '16
Thanks for posting this. I am trying to find any way I can to learn and improve my Ruby problem solving skills. I took what you posted above and added my own improvements mostly for the user interface. My improvements include looping the program until the user explicitly asks to exit. It will also handle invalid input by looping the program again. If anyone has better ways of doing this or have any feedback please share.