Noob question - Sending args to a method?

The following code leads to an error -

temp.rb:5:in `*': String can’t be coerced into Fixnum (TypeError)

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
Integer(temp)
c_f(temp)


Posted with NewsLeecher v3.9 Beta 1
Web @ NewsLeecher - The Complete Usenet Package


temp=gets.chomp
temp is still a string, as is all input read from stdin. Try replacing
that line with:
temp=gets.chomp.to_i

Dan

On 3/31/07, [email protected] [email protected] wrote:

temp=gets.chomp
Integer(temp)
c_f(temp)

Calling Integer(temp) does not modify temp. I think you mean something
like:

c_f(Integer(temp)) or c_f(temp.to_i)

Ryan

[email protected] 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”.

thanks all, i mistakenly believed that Integer() cast the variable to an
integer allowing it to be
passed. Is there another version of the gets method, like iput and
raw_input in Python?

The following code leads to an error -

temp.rb:5:in `*': String can’t be coerced into Fixnum (TypeError)

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
Integer(temp)
c_f(temp)


Posted with NewsLeecher v3.9 Beta 1
Web @ NewsLeecher - The Complete Usenet Package