dwyer@o2.ie wrote:
The following code leads to an error -
temp.rb:5:in `*': String can't be coerced into Fixnum (TypeError)
Apparently, you are trying to do math with a string.
can someone explain the error to me so I can determine how to resolve it, Thanks a million!
def c_f(temp)
convert=((9/5)*temp)+32
print "the coverted fig is ", convert
end
temp=gets.chomp
temp is assigned as a string, including endlines (\n) by gets.
Depending on whether you want a float (and seeing as that you are trying to create a temperature converter, you probably want that),
add the follwing line:
temp = temp.to_f
(either within your def ... end, or just after you get the input).
This converts the string into type Float.
To play safe, you might want to change the 9, 5, and 32 into 9.0,5.0, and 32.0, respectively.
Since Ruby isn't static typed, you have to convert your variables, if you get them from "outside" your program (exceptions exists, off the top of my head, YAML is such an exeption).
···
--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan.110mb.com/
Rule of Open-Source Programming #11:
When a developer says he will work on something, he or she means
"maybe".