[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie problem: String can't be coerced into Fixnum

Jason Bornhoft

12/27/2006 5:05:00 PM

I am brand new to Ruby and I would greatly appreciate any help you guys
can provide!

If this is not the correct place to post them please let me know where I
should post these types of questions.

This very short program is just supposed to take today's date using t =
Time.now and calculating what year the user was born...

Code:
puts "How old are you?"
age = gets.chomp

puts name + " is " + age + " years old."

t = Time.now
born = t.year - age

puts "You were probably born in " + born + "."


Thank you!!!

--
Posted via http://www.ruby-....

7 Answers

Jon Garvin

12/27/2006 5:20:00 PM

0

You need to convert the age string to a number. If you're coming from
Perl, you're used to this being done for you magically. Ruby expects
you to do it. Try this...

born = t.year - age.to_i



Ja Bo wrote:
> I am brand new to Ruby and I would greatly appreciate any help you guys
> can provide!
>
> If this is not the correct place to post them please let me know where I
> should post these types of questions.
>
> This very short program is just supposed to take today's date using t =
> Time.now and calculating what year the user was born...
>
> Code:
> puts "How old are you?"
> age = gets.chomp
>
> puts name + " is " + age + " years old."
>
> t = Time.now
> born = t.year - age
>
> puts "You were probably born in " + born + "."
>
>
> Thank you!!!
>
>


dblack

12/27/2006 5:20:00 PM

0

dblack

12/27/2006 5:23:00 PM

0

Cesar Rabak

12/27/2006 5:28:00 PM

0

Ja Bo escreveu:
> I am brand new to Ruby and I would greatly appreciate any help you guys
> can provide!
>
> If this is not the correct place to post them please let me know where I
> should post these types of questions.
>
> This very short program is just supposed to take today's date using t =
> Time.now and calculating what year the user was born...
>
> Code:
> puts "How old are you?"
> age = gets.chomp
>
> puts name + " is " + age + " years old."
>
> t = Time.now
> born = t.year - age
>
> puts "You were probably born in " + born + "."
>
>
You already got a lot of help from the Ruby interpreter. For grasping
the meaning of the error message, go to the "Pragmatic Programmer's
Guide" in Standard Types and search about the method #to_i.

HTH

--
Cesar Rabak

lrlebron@gmail.com

12/27/2006 5:29:00 PM

0


You have to make sure that the variable types are correct. Here's a
revised version of your script

name = "My name" # This variable was missing in the original code

puts "How old are you?"
age = gets.chomp # gets are a string by default

puts name + " is " + age + " years old."

t = Time.now
born = t.year-age.to_i # convert age to an integer

puts "You were probably born in " + born.to_s + "." #convert born to a
string

Hope this helps

Luis

Ja Bo wrote:
> I am brand new to Ruby and I would greatly appreciate any help you guys
> can provide!
>
> If this is not the correct place to post them please let me know where I
> should post these types of questions.
>
> This very short program is just supposed to take today's date using t =
> Time.now and calculating what year the user was born...
>
> Code:
> puts "How old are you?"
> age = gets.chomp
>
> puts name + " is " + age + " years old."
>
> t = Time.now
> born = t.year - age
>
> puts "You were probably born in " + born + "."
>
>
> Thank you!!!
>
> --
> Posted via http://www.ruby-....

Jason Bornhoft

12/27/2006 5:48:00 PM

0

Thank you very much for all of your help!!!

--
Posted via http://www.ruby-....

Kenosis

12/27/2006 7:34:00 PM

0


Ja Bo wrote:
> Thank you very much for all of your help!!!
>
> --
> Posted via http://www.ruby-....

And if you don't have a hard copy of the Pick Axe book you can access a
soft copy here:

http://www.rubycentral.com/book/...

Although I highly recommend you purchase the second edition :)

Ken