Forum: Ruby Newbie help, please

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.
Mike W. (Guest)
on 2009-04-26 23:47
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!
Mike W. (Guest)
on 2009-04-27 00:01
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!
Rick D. (Guest)
on 2009-04-27 00:02
(Received via mailing list)
On Sun, Apr 26, 2009 at 3:47 PM, Mike W. <removed_email_address@domain.invalid>
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.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
David M. (Guest)
on 2009-04-28 01:58
(Received via mailing list)
On Sunday 26 April 2009 15:01:37 Mike W. wrote:
> 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.
This topic is locked and can not be replied to.