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/
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}"
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.
···
On Fri, May 18, 2007 at 11:24:19PM +0900, Sebastian Hungerecker wrote:
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}"
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of others
we should be glad of an Opportunity to serve others by any Invention of
ours, and this we should do freely and generously."
> 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.
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
···
On 5/18/07, Felipe Contreras <felipe.contreras@gmail.com> wrote:
On 5/18/07, jim o <jamesoyim@yahoo.com> wrote:
--
Felipe Contreras
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw