John Joyce
4/3/2007 11:29:00 PM
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.
On Apr 4, 2007, at 5:15 AM, Merrie wrote:
> James, thank you very much for the help!
>
> Merrie S ----- Original Message ----- From: "James Edward Gray II"
> <james@grayproductions.net>
> To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
> Sent: Tuesday, April 03, 2007 4:11 PM
> Subject: Re: Question - Numbers & Variables
>
>
>> 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 Gray II
>>
>