[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie help, please

Mike West

4/26/2009 7:48:00 PM

Hello all,

I am a complete novice to programming. I have chosen Ruby as my first
language.

I am attempting to follow some tutorials in a book I have. However, I am
running into some errors, and can not figure out why. Can you please
guide me?

It appears I am having trouble mixing both integers with strings. For
example, if I try:

puts 2+4 ' is the sum of 2+4.'

I get an error.

In relation to the current exercise I am doing I am attempting to ask a
user for a number, and then have the computer tell them a bigger number
is better. Below is my attempted code:

puts 'Hello, what is your favorite number?'
number = gets.chomp
better = number.to_i+1
puts number + '?? Don\t you know that ' + better + 'is a superior
selection?'


That does not work though. However if I do:

puts 'Hello, what is your favorite number?'
number = gets.chomp
better = number.to_i+1
puts better

That works. But I would like a string to go along with the answer. How
do you mix arithmetic(or integers) and strings together?



Thank you!
--
Posted via http://www.ruby-....

3 Answers

Rick DeNatale

4/26/2009 8:01:00 PM

0

On Sun, Apr 26, 2009 at 3:47 PM, Mike West <bigfootm@mindspring.com> wrote:
> Hello all,
>
> I am a complete novice to programming. I have chosen Ruby as my first
> language.
>

> How do you mix arithmetic(or integers) and strings together?

Probably the most idiomatic way is to use string interpolation.

Within a double quoted string you can put a Ruby expression in #{ }
delimiters. The expression will be evaluated and to_s will be sent to
the result replacing the delimited expression so:

Instead of

>> puts 2+4 ' is the sum of 2+4.'

I would probably use:
puts "#{2+4} is the sum of 2+4"

and for

> puts number + '?? Don\t you know that ' + better + 'is a superior selection?'

puts "#{number}'?? Don\t you know that #{better} is a superior selection?"

--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Mike West

4/26/2009 8:02:00 PM

0

Of course after I post, I figure out the solution!

puts 'Hello, what is your favorite number?'
number = gets.chomp
better = number.to_i+1
puts number.to_s + '?? Aren\'t you aware that ' + better.to_s + ' is a
far better choice?'


Is that the best way to accomplish this? You can't mix strings and
integers- at all? Period?

Thank you!
--
Posted via http://www.ruby-....

David Masover

4/27/2009 9:58:00 PM

0

On Sunday 26 April 2009 15:01:37 Mike West wrote:
> Of course after I post, I figure out the solution!
>
> puts 'Hello, what is your favorite number?'
> number = gets.chomp
> better = number.to_i+1
> puts number.to_s + '?? Aren\'t you aware that ' + better.to_s + ' is a
> far better choice?'
>
>
> Is that the best way to accomplish this? You can't mix strings and
> integers- at all? Period?

As others have said, the best way is most likely string interpolation. I'd do
it like this:

number = gets.chomp.to_i
puts "#{number}?? Aren't you aware that #{number+1} is a far better choice?"

The double-quotes allow interpolation. The following is roughly equivalent,
but will likely execute slower:

number = gets.chomp.to_i
puts number.to_s + '?? Aren\'t you aware that ' + (number+1).to_s + ' is a far
better choice?'

Or, another way:

print number
print '?? Aren\'t you aware that '
print number + 1
puts ' is a far better choice?'

You absolutely can mix them, you just have to explicitly convert them, or use
them in a context where this is done for you. For instance, 'print' and 'puts'
will call to_s on whatever you pass them. 'p' will call inspect on whatever
you pass it.