# Begineer question

**URL:** <https://www.ruby-forum.com/t/begineer-question/104480>\
**Category:** Ruby\
**Created:** [May 18, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/begineer-question/104480 "2007-05-18T16:15:29Z")\
**Posts on this page:** 9\
**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:15pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/1 "2007-05-18T16:15:29Z")

</div>

I have had a horrible time googling this as I get too many hits back  
that don’t apply.

I am new to Ruby, and trying to find a good ref as for when one would  
use the form

```
puts #{a}

```

vs  
puts a

Does anyone have any pointers?

Thanks  
Jim

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_Looking  
for a deal? Find great prices on flights and hotels with Yahoo!  
FareChase.  
[http://farechase.yahoo.com/](http://farechase.yahoo.com/)

---

<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:21pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/2 "2007-05-18T16:21:30Z")

</div>

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.

---

<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:25pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/3 "2007-05-18T16:25:30Z")

</div>

jim o wrote:

> I am new to Ruby, and trying to find a good ref as for when one would use  
> the form
> 
> ```
> puts #{a}
> 
> ```
> 
> vs  
> puts a

puts #{a} doesn’t do anything (except return nil), so I’m going to  
assume you  
meant to say puts “#{a}”. Since that does the same thing as puts a but  
is more  
to type, I’d always use the latter. The #{} syntax is only useful when  
you  
want to print out more than just the content of the variable. For  
example:  
puts “The value of x is #{x}”

---

<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, 9:41pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/4 "2007-05-18T21:41:14Z")

</div>

On Fri, May 18, 2007 at 11:24:19PM +0900, Sebastian H. wrote:

> to type, I’d always use the latter. The #{} syntax is only useful when you  
> want to print out more than just the content of the variable. For example:  
> puts “The value of x is #{x}”

It really takes a more complex example to really make using that syntax  
worthwhile. After all, these are equivalent:

puts “The value of foo is #{foo}”  
puts "The falue of foo is " + foo

. . . except that the second example doesn’t require as much use of the  
Shift key.

Yeah, though – your example does make the point clear. I guess I’m  
just being a touch pedantic.

---

<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:34pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/5 "2007-05-18T17:34:40Z")

</div>

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

> 

This will explain a little more about what Sebastian showed you.

[http://www.rubycentral.com/book/tut\_stdtypes.html#S2](http://www.rubycentral.com/book/tut_stdtypes.html#S2)

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<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, 9:46pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/6 "2007-05-18T21:46:15Z")

</div>

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

> > 
> 
> 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.  
> Well maybe it might be useful to explain things a little more in  
> detail, because there is #to\_s called all over the place  
> As a matter of fact “#{a}” is the same as “” \<\< a.to\_s  
> and IO#puts, IO#write and IO#print convert their arguments by  
> calling#to\_s on them too.

It is therefore only in the context of e.g. puts that  
“#{a}” is the same as a.

HTH  
Robert

---

<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, 9:51pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/7 "2007-05-18T21:51:15Z")

</div>

Chad P. wrote:

> After all, these are equivalent:
> 
> puts “The value of foo is #{foo}”  
> puts "The falue of foo is " + foo

Only if foo is a string. #{} automatically to\_ses non-strings, while +  
doesn’t

---

<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 20, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/8 "2007-05-20T20:08:00Z")

</div>

On 5/20/07, Lloyd L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Perhaps a translation would make things clearer:
> 
> printf(“We are going to %s.”, [toUpper(destination)]);
> 
> puts “We are going to #{destination.upcase}.”  
> Maybe print is better a translation, but you made your point nonetheless  
> 😉

Robert

---

<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 20, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/begineer-question/104480/9 "2007-05-20T15:46:24Z")

</div>

Perhaps a translation would make things clearer:

printf(“We are going to %s.”, [toUpper(destination)]);

puts “We are going to #{destination.upcase}.”
