# A little problem

**URL:** <https://www.ruby-forum.com/t/a-little-problem/192745>\
**Category:** Ruby\
**Created:** [August 4, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/a-little-problem/192745 "2010-08-04T21:57:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/a-little-problem/192745/1 "2010-08-04T21:57:06Z")

</div>

Hi  
What is difference between these 2 codes ?  
(when I executed these codes result was the same )

* * *

def welcome1( name )  
puts “Hello#{name}”  
end

def welcome2( name )  
puts “Hello”+name  
end

welcome1( Amir )  
welcome2( Amir )

* * *

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:12pm UTC](https://www.ruby-forum.com/t/a-little-problem/192745/2 "2010-08-04T22:12:55Z")

</div>

In the first function, you’re performing interpolation on the variable;  
i.e. the #{} construct, when included inside “”, indicates to the  
interpreter to evaluate it as code, not text.

In the second function, you’re performing concatenation of the string  
“Hello” with the value of the variable name. In this case, the  
interpolation is automatic since the variable is outside the “”.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/a-little-problem/192745/3 "2010-08-04T22:16:26Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 04.08.2010 21:57, schrieb Amir E.:

> puts “Hello”+name  
> end
> 
> welcome1( Amir )  
> welcome2( Amir )
> 
> * * *

Try this:

welcome1(3)  
welcome2(3)

You’ll see the difference. It’s because a string interpolation as in  
your first example implicitly calls #to\_s on it’s result.

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.10 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJMWcoKAAoJEDJbypbtvoeic8sH/2xCN2tgVbPe4r876DL/qzp9  
1yy7s3mp/42o4C7D4UjRTR1FLZ8gi34EBJy544CAAgOYzBscE0sR7hqp2WxHEPjt  
WjuB0DTDCBimRF4KJYft2gCwK7ilvTWQMShQYIykS8BHH+KDF7oVLU6W0RjQGwbu  
2rCtaHmO68DLIZK/YCBhNP/5T/MVf6jv+9o6KPsWzBTCirsr0G4IiiN9ME98KMoM  
tI8A7wLOfdhX1K97yJtGEcrhObtNWLKdy8b/mkoK5ejN2+4WPhLVLmhzez3V/X1d  
11277TdGvmmpWBBfPfmJ0FJeca78mb5td5zaFhIhTbf7hUrpH6rXII+0xWU9oK4=  
=NW9K  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/a-little-problem/192745/4 "2010-08-04T22:18:12Z")

</div>

Amir E. wrote:

> puts “Hello”+name  
> end
> 
> welcome1( Amir )  
> welcome2( Amir )
> 
> * * *

The first calls #to\_s and the second calls #to\_str. The #to\_str method  
is usually only defined for objects that are already String-like. The  
#to\_s is usually defined for everything.

def welcome1( name )  
puts “Hello#{name}”  
end

def welcome2( name )  
puts “Hello”+name  
end

Amir = Object.new

def Amir.to\_s  
puts “to\_s”  
“Amir”  
end

def Amir.to\_str  
puts “to\_str”  
“Amir”  
end

welcome1( Amir )  
welcome2( Amir )
