Question - Numbers & Variables


#1

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.’


#2

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


#3

James, thank you very much for the help!

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


#4

On Apr 3, 2:11 pm, James Edward G. II 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.”


#5

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.


#6

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
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. :wink:

James Edward G. II