[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Noob question - Sending args to a method?

JervisTown(dwyer

3/31/2007 8:44:00 PM

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 @ http://www.newsleecher.c...
------------------- ----- ---- -- -

4 Answers

Dan Zwell

3/31/2007 8:57:00 PM

0

> 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

dwyer@o2.ie wrote:
> 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 @ http://www.newsleecher.c...
> ------------------- ----- ---- -- -
>
>
>

Ryan Leavengood

3/31/2007 8:59:00 PM

0

On 3/31/07, dwyer@o2.ie <JervisTown@ruby-lang.org> wrote:
> 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)

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

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

Ryan

Phillip Gawlowski

3/31/2007 9:04:00 PM

0

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....

Rule of Open-Source Programming #11:

When a developer says he will work on something, he or she means
"maybe".

JervisTown(dwyer

3/31/2007 9:45:00 PM

0

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 @ http://www.newsleecher.c...
------------------- ----- ---- -- -