# Adding string and numbers

**URL:** <https://www.ruby-forum.com/t/adding-string-and-numbers/146833>\
**Category:** Ruby\
**Created:** [August 26, 2008, 2:07am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833 "2008-08-26T02:07:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 2:07am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/1 "2008-08-26T02:07:40Z")

</div>

i’m new at programing. and i was trying to figure out how you add  
numbers. or string? here is the coding

puts ‘what is your first name?’  
name1= gets.chomp  
puts ‘how about your middle?’  
name2= gets.chomp  
puts ‘and what about your last name?’  
name3= gets.chomp  
puts 'did you know there are ‘+name1.length+name2.length+name3.length+ ’  
letters in your name?’

the problem is i get the strings combined, not adding the seperate  
numbers together.

help?

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 2:40am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/2 "2008-08-26T02:40:33Z")

</div>

Adam I. wrote:

> puts 'did you know there are ‘+name1.length+name2.length+name3.length+ ’  
> letters in your name?’
> 
> the problem is i get the strings combined, not adding the seperate  
> numbers together.

Try putting parentheses around the addition, like this:

(name1.length+name2.length+name3.length).to\_s

-Dana

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 2:50am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/3 "2008-08-26T02:50:27Z")

</div>

Dana M. wrote:

> Adam I. wrote:
> 
> > puts 'did you know there are ‘+name1.length+name2.length+name3.length+ ’  
> > letters in your name?’
> > 
> > the problem is i get the strings combined, not adding the seperate  
> > numbers together.
> 
> Try putting parentheses around the addition, like this:
> 
> (name1.length+name2.length+name3.length).to\_s
> 
> -Dana

hurray that worked!

im not quite sure why it worked though? doesnt it need to be integers to  
add together?

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 2:59am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/4 "2008-08-26T02:59:50Z")

</div>

On Mon, Aug 25, 2008 at 7:03 PM, Adam I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> letters in your name?’
> 
> the problem is i get the strings combined, not adding the seperate  
> numbers together.
> 
> help?

name1.length and friends are numbers, not strings. You might have to  
you use to\_s.

Here’s a fun inject exercise (q means array of questions, a means  
array of answers)…

q = [“what is your first name?”, “how about your middle name?”, “and  
what about your last name?”]  
puts q.inject([]) {|a, i| puts i; a \<\< gets.chomp}.inject(0) {|s, a| s +  
a.size}

Todd

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/5 "2008-08-26T17:39:56Z")

</div>

On Mon, Aug 25, 2008 at 7:59 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > puts 'did you know there are '+name1.length+name2.length+name3.length+ ’
> 
> Here’s a fun inject exercise (q means array of questions, a means  
> array of answers)…
> 
> q = [“what is your first name?”, “how about your middle name?”, “and  
> what about your last name?”]  
> puts q.inject([]) {|a, i| puts i; a \<\< gets.chomp}.inject(0) {|s, a| s + a.size}

To comply with your original requirements, that last line should be…

puts ‘did you know there are ’ + q.inject([]) {|a, i| puts i; a \<\<  
gets.chomp}.inject(0) {|s, a| s + a.size}.to\_s + ’ letters in your  
name?’

Very strange looking, but makes the most logical sense to me.

Todd

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 3:22am UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/6 "2008-08-26T03:22:05Z")

</div>

On Mon, Aug 25, 2008 at 7:46 PM, Adam I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > (name1.length+name2.length+name3.length).to\_s
> > 
> > -Dana
> 
> hurray that worked!
> 
> im not quite sure why it worked though? doesnt it need to be integers to  
> add together?

(integer + integer + integer).to\_s

Inside the parentheses, those are Integer instances being added  
together, then the to\_s turns the sum into a String object so it will  
cooperate with the surrounding strings.

I hope that makes some sense. Sometimes, I don’t even know what I’m  
saying 🙂

Todd

---

<div class="post-metadata">

**Author:** ![iggysx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@iggysx](https://www.ruby-forum.com/u/iggysx)\
**Post date:** [August 26, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/adding-string-and-numbers/146833/7 "2008-08-26T17:41:42Z")

</div>

On Mon, Aug 25, 2008 at 7:59 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > puts 'did you know there are '+name1.length+name2.length+name3.length+ ’
> 
> Here’s a fun inject exercise (q means array of questions, a means  
> array of answers)…
> 
> q = [“what is your first name?”, “how about your middle name?”, “and  
> what about your last name?”]  
> puts q.inject([]) {|a, i| puts i; a \<\< gets.chomp}.inject(0) {|s, a| s + a.size}

To comply with your original requirements, that last line should be…

puts ‘did you know there are ’ + q.inject([]) {|a, i| puts i; a \<\<  
gets.chomp}.inject(0) {|s, a| s + a.size}.to\_s + ’ letters in your  
name?’

Very strange looking, but makes the most logical sense to me.

Todd
