Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie problem: String can't be coerced into Fixnum

Reply
Thread Tools

Newbie problem: String can't be coerced into Fixnum

 
 
Ja Bo
Guest
Posts: n/a
 
      12-27-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Jon Garvin
Guest
Posts: n/a
 
      12-27-2006
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!!!
>
>



 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      12-27-2006
Hi --

On Thu, 28 Dec 2006, 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


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




David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      12-27-2006
Hi --

On Thu, 28 Dec 2006, wrote:

> Hi --
>
> On Thu, 28 Dec 2006, 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

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


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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Cesar Rabak
Guest
Posts: n/a
 
      12-27-2006
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
 
Reply With Quote
 
lrlebron@gmail.com
Guest
Posts: n/a
 
      12-27-2006

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-forum.com/.


 
Reply With Quote
 
Ja Bo
Guest
Posts: n/a
 
      12-27-2006
Thank you very much for all of your help!!!

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

 
Reply With Quote
 
Kenosis
Guest
Posts: n/a
 
      12-27-2006

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

Ken

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nil can't be coerced into Fixnum (TypeError) Mayank K. Ruby 3 03-24-2011 07:09 AM
Float Arithmetic: Return A Fixnum When Float == Fixnum MaggotChild Ruby 6 12-02-2009 04:28 AM
`+': Range can't be coerced into Fixnum (TypeError) Prateek Agarwal Ruby 4 07-31-2009 08:08 PM
Why Fixnum===Fixnum is false? Heesob Park Ruby 5 05-14-2009 12:31 AM
array collect - nil can't be coerced into Float (TypeError) Jason Lillywhite Ruby 6 06-11-2008 08:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57