[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Beginner question

jim o

5/18/2007 2:26:00 PM

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 Contreras <felipe.contreras@gmail.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Friday, May 18, 2007 9:20:19 AM
Subject: Re: Begineer question

On 5/18/07, jim o <jamesoyim@yahoo.com> wrote:
> 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?

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 Contreras







____________________________________________________________________________________Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid...


3 Answers

Avdi Grimm

5/18/2007 2:33:00 PM

0

On 5/18/07, jim o <jamesoyim@yahoo.com> 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

--
Avdi

Karl von Laudermann

5/18/2007 2:34:00 PM

0

On May 18, 10:26 am, jim o <jameso...@yahoo.com> 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

Sebastian Hungerecker

5/18/2007 2:38:00 PM

0

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.


--
Ist so, weil ist so
Bleibt so, weil war so