# Beginner with a question

**URL:** <https://www.ruby-forum.com/t/beginner-with-a-question/111193>\
**Category:** Ruby\
**Created:** [August 3, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193 "2007-08-03T18:49:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/1 "2007-08-03T18:49:35Z")

</div>

So I am new to ruby, and I seem to be stuck on a seemingly simple  
problem. I  
have two variable’s, one that is defined in the script, and one that is  
grabbed from input. I can add them together just fine, but I have not  
come  
up with a way to put the addition statement inline with strings. For  
instance:

```
     puts favnum.to_i + plus1.to_i

```

Works producing the expected result. However when I try something like  
this:

```
     puts 'Try ' + favnum + plus1 + ' has the result.'

```

I get errors. I have tried declaring the variables as integers, and  
tried  
various syntax’s on that line, but the solution is escaping me and I  
have  
not found a good way to do this. I would also be willing to do a  
variable to  
define the item I want to add into the line, such as:

```
    finalnum = favnum.to_i + plus1.to_i

```

But that doesnt produce the desired result either as the following  
doesnt  
work:

```
    puts 'Try '+ finalnum +' instead.'

```

In both cases I also tried forcing .to\_i but that didnt help. (though  
doing  
a “puts finalnum” works.)

So what sort of silly syntactical error am I making ?

(Apologies if this is the wrong place to post this, it seemed like my  
best  
bet.)

~Clockwork

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/2 "2007-08-03T19:04:36Z")

</div>

On Aug 3, 2007, at 11:48 AM, removed\_email\_address@domain.invalid wrote:

> variable to  
> In both cases I also tried forcing .to\_i but that didnt help.  
> (though doing  
> a “puts finalnum” works.)
> 
> So what sort of silly syntactical error am I making ?
> 
> (Apologies if this is the wrong place to post this, it seemed like  
> my best  
> bet.)
> 
> ~Clockwork  
> Very simple actually,  
> read those error messages!  
> You’re trying to do math and concatenation of strings at the same time.  
> so you have some options.  
> One of them is using .to\_s on those numbers to turn them into strings.  
> But if you wan the result of adding or subtracting or what have you,  
> then you need parentheses around the math part, followed by .to\_s

puts ‘Try ’ + (favnum + plus1).to\_s + ’ has the result.’

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/3 "2007-08-03T19:06:37Z")

</div>

It seems like you are coming from Perl or PHP where such expressions are  
converted for you. This is not the case in Ruby. The language may be  
dynamicly typed, but it’s strongly typed, so adding a FixNum to a String  
doesn’t make sense. Thus, you’re best off doing the following:

favnum = favnum.to\_i  
plus1 = plus1.to\_i

and continue with actual FixNum objects. Also, make libral use of string  
replacements:

puts “Try #{favnum + plus1} …”

Such a thing calls #to\_s on the result of whats in #{…}.

Welcome to Ruby!

Jason

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/4 "2007-08-03T19:12:38Z")

</div>

On 8/3/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So what sort of silly syntactical error am I making ?

Ruby does not implicitly convert Integers into Strings. You need to  
explicitly tell Ruby this is what you are doing.

puts “Try #{finalnum} instead”

Inside double quoted strings, anything inside a #{ } delimiter is  
treated as Ruby code, and it is evaluated by the interpreter and  
converted into a string. So you could also write (as you did  
originally)

puts “Try #{favnum + plus1} has the result.”

Hope that makes sense. Oh, and welcome to Ruby!

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/5 "2007-08-03T19:09:38Z")

</div>

When you are defining the one variable in the script are you defining it  
as  
a string? i.e. var = “3”

On 8/3/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Works producing the expected result. However when I try something like  
> finalnum = favnum.to\_i + plus1.to\_i  
> So what sort of silly syntactical error am I making ?
> 
> (Apologies if this is the wrong place to post this, it seemed like my best  
> bet.)
> 
> ~Clockwork

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/6 "2007-08-03T19:58:42Z")

</div>

Awesome. Thanks.

Whats the difference between single and double quoted strings in ruby ?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/beginner-with-a-question/111193/7 "2007-08-03T20:27:49Z")

</div>

On 8/3/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Awesome. Thanks.
> 
> Whats the difference between single and double quoted strings in ruby ?

Take a look at this section on Strings from the Programming Ruby book  
…

[http://www.rubycentral.com/pickaxe/language.html#UD](http://www.rubycentral.com/pickaxe/language.html#UD)

That will answer your question in much greater detail than I could in  
this short mailing list post.

Blessings,  
TwP
