# Need Help with a Very, Very Simple Program

**URL:** <https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224>\
**Category:** Ruby\
**Created:** [July 17, 2009, 1:53am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224 "2009-07-17T01:53:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 1:53am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/1 "2009-07-17T01:53:56Z")

</div>

I’m almost an absolute novice in Ruby; it is my first language and I  
just began learning the basics yesterday, using Chris P.'s ‘Learning  
to Program.’

The trouble I’m having is simple, but nonetheless I would appreciate an  
explanation. The little program I’ve written accomplishes a simple task:  
it asks for your favorite number, adds one to it, then returns it to  
you. Here it is:

puts ‘Hey there, I’m Uno.’  
puts ‘What’s your favorite number?’  
number = gets.chomp  
better = number.to\_i + 1  
puts better + ‘…That’s better.’

I’m receiving this error: String can’t be coerced into Fixnum  
(TypeError).’ Why can’t this variable–which contains a numeric  
string–be converted into an integer?

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 2:05am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/2 "2009-07-17T02:05:06Z")

</div>

Hi –

On Fri, 17 Jul 2009, Max N. wrote:

> I’m almost an absolute novice in Ruby; it is my first language and I  
> just began learning the basics yesterday, using Chris P.'s ‘Learning  
> to Program.’

Welcome to Ruby!

> I’m receiving this error: String can’t be coerced into Fixnum  
> (TypeError).’ Why can’t this variable–which contains a numeric  
> string–be converted into an integer?

The reason is that better is an integer, but “That’s better.” is a  
string. So it’s like you’re trying to do:

5 + “hello”

and that doesn’t work. You can convert back:

puts better.to\_s + “…That’s better”

or use Ruby’s string interpolation syntax, which inserts expressions  
into strings, via the #{} construct, and does the conversion for you:

puts “#{better}…That’s better.”

I’ve interpolated better, not better.to\_s, and it will automatically  
be to\_s’d for me.

David

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 2:05am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/3 "2009-07-17T02:05:41Z")

</div>

On Thu, Jul 16, 2009 at 4:53 PM, Max N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …  
> puts ‘Hey there, I'm Uno.’  
> puts ‘What's your favorite number?’  
> number = gets.chomp  
> better = number.to\_i + 1  
> puts better + ‘…That's better.’

You’re looking to the wrong line for the error

it’s

puts better.to\_s + …

John

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 2:09am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/4 "2009-07-17T02:09:10Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Fri, 17 Jul 2009, Max N. wrote:
> 
> > I’m almost an absolute novice in Ruby; it is my first language and I  
> > just began learning the basics yesterday, using Chris P.'s ‘Learning  
> > to Program.’
> 
> Welcome to Ruby!
> 
> > I’m receiving this error: String can’t be coerced into Fixnum  
> > (TypeError).’ Why can’t this variable–which contains a numeric  
> > string–be converted into an integer?
> 
> The reason is that better is an integer, but “That’s better.” is a  
> string. So it’s like you’re trying to do:
> 
> 5 + “hello”
> 
> and that doesn’t work. You can convert back:
> 
> puts better.to\_s + “…That’s better”
> 
> or use Ruby’s string interpolation syntax, which inserts expressions  
> into strings, via the #{} construct, and does the conversion for you:
> 
> puts “#{better}…That’s better.”
> 
> I’ve interpolated better, not better.to\_s, and it will automatically  
> be to\_s’d for me.
> 
> David

> Welcome to Ruby!  
> Thanks! I look forward to learning what appears to be an incredibly  
> dynamic language.

Ahh…I see. Thank you for that explanation. It’s instances such as  
these that allow a few basic concepts to come together.

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 5:40am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/5 "2009-07-17T05:40:06Z")

</div>

Hi,

On Thu, Jul 16, 2009 at 4:53 PM, Max N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m almost an absolute novice in Ruby; it is my first language and I  
> just began learning the basics yesterday, using Chris P.'s ‘Learning  
> to Program.’

Welcome!

> The trouble I’m having is simple, but nonetheless I would appreciate an  
> explanation. The little program I’ve written accomplishes a simple task:  
> it asks for your favorite number, adds one to it, then returns it to  
> you. Here it is:
> 
> puts ‘Hey there, I'm Uno.’  
> puts ‘What's your favorite number?’  
> number = gets.chomp  
> better = number.to\_i + 1  
> puts better + ‘…That's better.’

Looks like a few other folks have already solved your problem, so a  
quick note on Ruby idiom/features. Ruby lets you use single _or_  
double quotes to represent a string. The difference is that double  
quotes allow variables to easily be inserted in your string. Also,  
they make it easy to have apostrophes in your strings! A slightly  
modified example, with no more ' pain:

puts “Hey there, I’m Uno.”  
puts "What’s your favorite number?

number = gets.chomp  
better = number.to\_i + 1

puts “#{better}… That’s better.”

~ j.

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 17, 2009, 5:52am UTC](https://www.ruby-forum.com/t/need-help-with-a-very-very-simple-program/172224/6 "2009-07-17T05:52:57Z")

</div>

On Fri, Jul 17, 2009 at 11:39 AM, John wrote:

> On Thu, Jul 16, 2009 at 4:53 PM, Max N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m almost an absolute novice in Ruby; it is my first language and I  
> > …  
> > number = gets.chomp  
> > better = number.to\_i + 1
> 
> puts “#{better}… That’s better.”

to the OP:

you could try playing (just for ruby fun) w the above code; make it a  
one-liner, eg:

puts “#{gets.to\_i + 1}… That’s better.”

kind regards -botp
