Forum: Ruby Question - Numbers & Variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Df5eb31c904dc50d0a8d5682a1a328b3?d=identicon&s=25 Merrie (Guest)
on 2007-04-03 21:53
(Received via mailing list)
Im currently working through the book 'Learn to Program' by Chris Pine
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.'
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-03 22:12
(Received via mailing list)
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
Df5eb31c904dc50d0a8d5682a1a328b3?d=identicon&s=25 Merrie (Guest)
on 2007-04-03 22:16
(Received via mailing list)
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
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-04-04 01:30
(Received via mailing list)
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.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-04-04 15:52
(Received via mailing list)
On Apr 3, 2:11 pm, James Edward Gray II <j...@grayproductions.net>
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."
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-04 16:06
(Received via mailing list)
On Apr 4, 2007, at 8:50 AM, Phrogz wrote:

> On Apr 3, 2:11 pm, James Edward Gray II <j...@grayproductions.net>
> 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 Gray II
This topic is locked and can not be replied to.