Newbie help, please

M

Mike West

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

Rick DeNatale

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

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
 
M

Mike West

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

David Masover

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top