# Newbie help, please

**URL:** <https://www.ruby-forum.com/t/newbie-help-please/166523>\
**Category:** Ruby\
**Created:** [April 26, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/newbie-help-please/166523 "2009-04-26T21:47:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vh1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vh1](https://www.ruby-forum.com/u/vh1)\
**Post date:** [April 26, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/newbie-help-please/166523/1 "2009-04-26T21:47:43Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![vh1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vh1](https://www.ruby-forum.com/u/vh1)\
**Post date:** [April 26, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/newbie-help-please/166523/2 "2009-04-26T22:01:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![vh1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vh1](https://www.ruby-forum.com/u/vh1)\
**Post date:** [April 27, 2009, 11:58pm UTC](https://www.ruby-forum.com/t/newbie-help-please/166523/3 "2009-04-27T23:58:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vh1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vh1](https://www.ruby-forum.com/u/vh1)\
**Post date:** [April 26, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/newbie-help-please/166523/4 "2009-04-26T22:02:05Z")

</div>

On Sun, Apr 26, 2009 at 3:47 PM, Mike W. [removed\_email\_address@domain.invalid](mailto: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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
