Newbie problem: String can't be coerced into Fixnum

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

On 12/27/06, Ja Bo [email protected] wrote:

Code:

Thank you!!!


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

You were outputting a string, and adding a integer to the string.

name = “Jason”
puts “How old are you?”
age = gets.chomp

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

t = Time.now

born =(t.year - age.to_i)

puts "You were probably born in " + born.to_s + “.”

How old are you?
28
Jason is 28 years old.
You were probably born in 1978.

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

Hi –

On Thu, 28 Dec 2006, Ja Bo wrote:

puts “How old are you?”
age = gets.chomp

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

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

The problem there is that t.year is an integer and age is a string.
You need to convert age:

born = t.year - age.to_i # to_i is “to integer”

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

And don’t forget you can use string interpolation:

puts “You were probably born in #{born}.”

Or even do it all at once:

puts “You were probably born in #{Time.now.year - age.to_i}.”

:slight_smile:

David

Hi –

On Thu, 28 Dec 2006, [email protected] wrote:

This very short program is just supposed to take today’s date using t =

Or even do it all at once:

puts “You were probably born in #{Time.now.year - age.to_i}.”

:slight_smile:

And, as the other responses reminded me, if you do it the way you
were, you have to convert born to a string:

puts "You were probably born in " + born.to_s + “.”

With string interpolation, you don’t; the interpolation automatically
does the conversion.

David

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:

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


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

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/index.html

Although I highly recommend you purchase the second edition :slight_smile:

Ken

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