[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Question - Numbers & Variables

James Gray

4/3/2007 8:11:00 PM

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

4 Answers

Merrie

4/3/2007 8:16:00 PM

0

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
>

John Joyce

4/3/2007 11:29:00 PM

0

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


Gavin Kistner

4/4/2007 1:47:00 PM

0

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

James Gray

4/4/2007 2:04:00 PM

0

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