# Re: Beginner question

**URL:** <https://www.ruby-forum.com/t/re-beginner-question/104481>\
**Category:** Ruby\
**Created:** [May 18, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481 "2007-05-18T16:27:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481/1 "2007-05-18T16:27:31Z")

</div>

I guess I did not ask my question very clearly. Sorry about that.

What does the “#{ }” do for you? I think what I am missing is the  
feature provided by having that as a wrapper vs bare.

Does that make sense?  
Thanks  
Jim

----- Original Message ----  
From: Felipe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, May 18, 2007 9:20:19 AM  
Subject: Re: Begineer question

On 5/18/07, jim o [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> vs  
> puts a
> 
> Does anyone have any pointers?

You mean:  
puts “#{a}”

Right? If so then it simply helps to do:

puts “foo=#{a} allows you to do more interesting things”

If you just want to print ‘a’ then there’s no reason to do “#{a}” it  
would be like doing “%s” % [a]; you can do it, but it doesn’t make  
sense.

–  
Felipe C.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_Be  
a better Heartthrob. Get better relationship answers from someone who  
knows. Yahoo! Answers - Check it out.

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com/)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481/2 "2007-05-18T16:33:31Z")

</div>

On 5/18/07, jim o [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I did not ask my question very clearly. Sorry about that.
> 
> What does the “#{ }”

Using #{} in a string literal (something in between double quotes)  
interpolates the result of a block of code into the string. So:

```
puts "Four plus five equals #{4 + 5}"

```

means the same thing as:

```
puts "Four plus five equals " + (4 + 5).to_s

```

They both print:

```
Four plus five equals 9

```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481/3 "2007-05-18T16:35:31Z")

</div>

On May 18, 10:26 am, jim o [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I did not ask my question very clearly. Sorry about that.
> 
> What does the “#{ }” do for you? I think what I am missing is the feature provided by having that as a wrapper vs bare.

#{} in a string will interpret whatever is contained as ruby code,  
convert the result into a string, and insert it into the containing  
string. for example:

puts “Eight plus twelve equals #{8 + 12}”

will print:

Eight plus twelve equals 20

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481/4 "2007-05-18T16:39:31Z")

</div>

jim o wrote:

> What does the “#{ }” do for you? I think what I am missing is the feature  
> provided by having that as a wrapper vs bare.

You can’t use bare variables inside a string.  
x=7  
puts “The number is x”

This will return (of course) print out “The number is x” because ruby  
has  
no way of knowing that you actually wanted it to print out the value of  
the variable x instead of a literal x. If you however put #{} around the  
x,  
it will be substituted with the value of x.  
That’s the point of the #{}. There’s however no point to pass a string  
to puts  
that only contains a #{} because than you could as well just pass the  
variable  
to puts.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/re-beginner-question/104481/5 "2007-05-18T17:25:39Z")

</div>

hello Guys,

let me give another example. let say you want to create a greeting base  
on a  
person name. You will write:

def greetings(name)  
“Good Morning #{name.capitalize}”  
end

puts greetings(‘loi’)  
“Good Morning Loi”

as you can see the #{} allows you to create complex expressions. In this  
case you were able to capitilize the name Loi even though you entered it  
in  
lower case letter. So when the interpreter does the interpolation sees  
the  
method capitalize and capitalize the name for you.

I hope this example will help you

Carlos Henriquez
