# Question - Numbers & Variables

**URL:** <https://www.ruby-forum.com/t/question-numbers-variables/91024>\
**Category:** Ruby\
**Created:** [April 3, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024 "2007-04-03T21:53:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 3, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/1 "2007-04-03T21:53:07Z")

</div>

Im currently working through the book ‘Learn to Program’ by Chris P.  
and I am stuck on one of the exercises. I feel like Im close to the  
answer but just cannot quite get it. I appreciate the help. Below is the  
program.

What I would like to see is: What is your favorite number? 14 Well this  
15 (adding 1) might be better. The error is relating to to\_i but Im not  
sure where to place it on the num or the +1?

puts ‘What is your favorite number?’  
num = gets.chomp  
num=num +1  
puts ‘Well this ’ + num + ’ might be better.’

---

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 3, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/2 "2007-04-03T22:12:14Z")

</div>

On Apr 3, 2007, at 2:51 PM, Merrie wrote:

> What I would like to see is: What is your favorite number? 14 Well  
> this 15 (adding 1) might be better. The error is relating to to\_i  
> but Im not sure where to place it on the num or the +1?

There are two errors in the script. You need to help Ruby know when  
to convert the objects you are working with…

> puts ‘What is your favorite number?’  
> num = gets.chomp  
> num=num +1

num = num.to\_i + 1 # we have a String, but want an Integer

> puts ‘Well this ’ + num + ’ might be better.’

# now we need to go back to a String so we can concatenate

puts ‘Well this ’ + num.to\_s + ’ might be better.’

Does that help?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 3, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/3 "2007-04-03T22:16:14Z")

</div>

James, thank you very much for the help!

Merrie S  
----- Original Message -----  
From: “James Edward G. II” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, April 03, 2007 4:11 PM  
Subject: Re: Question - Numbers & Variables

---

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 4, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/4 "2007-04-04T15:52:10Z")

</div>

On Apr 3, 2:11 pm, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > puts ‘Well this ’ + num + ’ might be better.’
> 
> # now we need to go back to a String so we can concatenate
> 
> puts ‘Well this ’ + num.to\_s + ’ might be better.’

Also, I usually find it more convenient to use string interpolation,  
as it implicitly calls the to\_s method on the value inside it:  
puts “Well this #{num} might be better.”

---

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 4, 2007, 1:30am UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/5 "2007-04-04T01:30:27Z")

</div>

Another way to do the plus one. Or the next alphabetic character,  
is .next  
so, you could get pretty crazy with:  
num.next

something like so:

puts ‘What is your favorite number?’  
num = gets.chomp  
puts ‘Well this ’ + num.next + ’ might be better.’

in this case it goes to the next number, as a character, not as a  
number, but the result is the same and you don’t need to convert it.  
Not always what you want, but pretty nice example of how concise and  
convenient Ruby can be.

---

<div class="post-metadata">

**Author:** ![Merrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@Merrie](https://www.ruby-forum.com/u/Merrie)\
**Post date:** [April 4, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/question-numbers-variables/91024/6 "2007-04-04T16:06:14Z")

</div>

On Apr 4, 2007, at 8:50 AM, Phrogz wrote:

> On Apr 3, 2:11 pm, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > puts ‘Well this ’ + num + ’ might be better.’
> > 
> > # now we need to go back to a String so we can concatenate
> > 
> > puts ‘Well this ’ + num.to\_s + ’ might be better.’
> 
> Also, I usually find it more convenient to use string interpolation,  
> as it implicitly calls the to\_s method on the value inside it:  
> puts “Well this #{num} might be better.”

Me too, but Learn To Program doesn’t teach that (I wish it did!) and  
I didn’t want to confuse the issue. 😉

James Edward G. II
