# Simple newbie question

**URL:** <https://www.ruby-forum.com/t/simple-newbie-question/94686>\
**Category:** Ruby\
**Created:** [May 11, 2006, 2:13am UTC](https://www.ruby-forum.com/t/simple-newbie-question/94686 "2006-05-11T02:13:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [May 11, 2006, 2:13am UTC](https://www.ruby-forum.com/t/simple-newbie-question/94686/1 "2006-05-11T02:13:07Z")

</div>

Hi, I’m just starting Ruby and theres something I’m sure is very simple  
but I just cant get it work. Its an exercise in Chris P.'s tutorial.  
The question is:  
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.

My code:  
puts ‘What is your favourite number?’  
number = gets.chomp  
puts ‘I think ’ + (number.to\_s + 1.to\_s) + ’ is a better number’

is the only way I can get anything to work but this is adding strings so  
2 + 1 becomes 21 which is not the objective.

Of course, I need the answer to be 3.

puts ‘What is your favourite number?’  
number = gets.chomp  
puts ‘I think ’ + (number.to\_i + 1.to\_i) + ’ is a better number’

results in this error “cannot convert Fixnum into String (TypeError)”

I’ve tried lots of other combinations but its not happening for me.  
I know its simple but, hey, I have to start somewhere.

Bob

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [May 11, 2006, 2:24am UTC](https://www.ruby-forum.com/t/simple-newbie-question/94686/2 "2006-05-11T02:24:03Z")

</div>

From: “Bob W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> puts ‘What is your favourite number?’  
> number = gets.chomp  
> puts ‘I think ’ + (number.to\_i + 1.to\_i) + ’ is a better number’
> 
> results in this error “cannot convert Fixnum into String (TypeError)”

One way:

puts ‘I think ’ + (number.to\_i + 1).to\_s + ’ is a better number’

Another way (note double quotes instead of single quotes):

puts “I think #{number.to\_i + 1} is a better number”

Hope this helps,

Bill

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [May 11, 2006, 2:24am UTC](https://www.ruby-forum.com/t/simple-newbie-question/94686/3 "2006-05-11T02:24:08Z")

</div>

On May 10, 2006, at 8:13 PM, Bob W. wrote:

> number = gets.chomp  
> puts ‘I think ’ + (number.to\_i + 1.to\_i) + ’ is a better number’

You can call methods on expressions…

( some code ).to\_s

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [May 11, 2006, 2:33am UTC](https://www.ruby-forum.com/t/simple-newbie-question/94686/4 "2006-05-11T02:33:45Z")

</div>

> One way:
> 
> puts ‘I think ’ + (number.to\_i + 1).to\_s + ’ is a better number’
> 
> Another way (note double quotes instead of single quotes):
> 
> puts “I think #{number.to\_i + 1} is a better number”
> 
> Hope this helps,
> 
> Bill

Thanks Bill and Logan,  
The fog is lifting a little now.

Bob
