# What am i doing wrong?

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808>\
**Category:** Ruby\
**Created:** [January 21, 2007, 7:36am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808 "2007-01-21T07:36:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 7:36am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/1 "2007-01-21T07:36:02Z")

</div>

I realize this is an extremely noobish question, but i’m stuck and can’t  
seem to figure it out (aside from some very simple HTML, i know nothing  
about programming).

I’m trying to follow Chris P. guide here:  
[http://pine.fm/LearnToProgram/?Chapter=04](http://pine.fm/LearnToProgram/?Chapter=04)

I’m trying this question he suggested:  
â€¢ Write a program which asks for a person’s favorite number. Have your  
program add one to the number, then suggest the result as a bigger and  
better favorite number. (Do be tactful about it, though.)

This is what i have, but it’s not working:

puts ‘What is your favorite number?’  
number = gets  
puts ‘That is a good number, but ’ + (number.to\_i + 1) + ’ is better!’

However, if i remove the (number.to\_i + 1) from that line, it prints out  
the text just fine. And if i remove the text, so the line is only:

puts number.to\_i + 1

I get the correct results. But if i try to combine the two, i get:  
TypeError: cannot convert Fixnum into String

method + in untitled document at line 3  
at top level in untitled document at line 3

I’m using Textmate if that matters.

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 7:49am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/2 "2007-01-21T07:49:24Z")

</div>

Nick,

> number = gets

after this line, try to

p number

to see what does it contain. It contains

“4\n”

So, there is an ugly “\n” (carriage return) at the end. Let’s remove  
that!

number = gets.chop  
p number

now we have

“4”

That’s better.  
However, this was not the problem, your program will run also without  
this fix 🙂 The problem is that

(number.to\_i + 1)

is a FixNum - you need to turn that into a String. So the correct  
solution reads:

‘That is a good number, but ’ + (number.to\_i + 1).to\_s + ’ is better!’

HTH,  
Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 7:49am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/3 "2007-01-21T07:49:24Z")

</div>

(number.to\_i + 1).to\_s

You’re trying to merge a String and a Fixnum into one object (i.e.,  
you feed one big string obect to puts; you can’t merge a Fixnum and  
String like that). The to\_s method will convert the Fixnum you  
created with to\_i and added one to back to a string for output.

Kind of confusing I know, but if you got this far, I’m sure youll get  
it. 🙂

–Jeremy

On 1/21/07, Nick N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> better favorite number. (Do be tactful about it, though.)  
> puts number.to\_i + 1  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 8:16am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/4 "2007-01-21T08:16:26Z")

</div>

Another alternative:

puts “What’s your favourite number?”  
number= gets.to\_i  
puts “Hmm… not bad, but #{number+1} is better”

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 7:55am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/5 "2007-01-21T07:55:56Z")

</div>

Thank you very much! That does make sense now!

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 8:43am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/6 "2007-01-21T08:43:28Z")

</div>

Nick N. wrote:

> better favorite number. (Do be tactful about it, though.)  
> puts number.to\_i + 1
> 
> I get the correct results. But if i try to combine the two, i get:  
> TypeError: cannot convert Fixnum into String

printf “That is good, but %d is better.\n”, number.to\_i+1

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 9:34am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/7 "2007-01-21T09:34:31Z")

</div>

> print "What’s your favorite number? "  
> puts “Good one, but #{gets.to\_i + 1} is better”

Nice (I didn’t realise you could do that either).  
How about with error checking:

print "What’s your favorite number? "  
puts “Good one, but #{Float(gets)+1} is better” rescue puts “That’s  
not a number!”

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 9:15am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/8 "2007-01-21T09:15:31Z")

</div>

Sharon P. wrote the following on 21.01.2007 08:16 :

> Another alternative:
> 
> puts “What’s your favourite number?”  
> number= gets.to\_i  
> puts “Hmm… not bad, but #{number+1} is better”

Just realised this one works:

print "What’s your favorite number? "  
puts “Good one, but #{gets.to\_i + 1} is better”

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [January 21, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/82808/9 "2007-01-21T21:27:55Z")

</div>

On Jan 21, 2007, at 12:48 AM, Peter S. wrote:

> “4\n”
> 
> So, there is an ugly “\n” (carriage return) at the end. Let’s  
> remove that!
> 
> number = gets.chop  
> p number

When you want to remove a line ending, prefer chomp() to chop().  
chomp() will only target the line ending, which will cause you less  
trouble when using it on a line that doesn’t actually have one:

> > “James”.chomp  
> > =\> “James”
> 
> > “James”.chop  
> > =\> “Jame”

Finally, neither is needed in this case:

> > “42\n”.chomp.to\_i == “42\n”.to\_i  
> > =\> true

James Edward G. II
