# Noob question - Sending args to a method?

**URL:** <https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750>\
**Category:** Ruby\
**Created:** [March 31, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750 "2007-03-31T22:51:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750/1 "2007-03-31T22:51:24Z")

</div>

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](http://www.newsleecher.com/?usenet)

* * *

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750/2 "2007-03-31T22:57:25Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750/3 "2007-03-31T23:00:25Z")

</div>

On 3/31/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750/4 "2007-03-31T23:04:25Z")

</div>

removed\_email\_address@domain.invalid 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/](http://cynicalryan.110mb.com/)

Rule of Open-Source Programming #11:

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/noob-question-sending-args-to-a-method/90750/5 "2007-03-31T23:45:50Z")

</div>

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](http://www.newsleecher.com/?usenet)

* * *
