Forum: Ruby Begineer question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
jim o (Guest)
on 2007-05-18 18:15
(Received via mailing list)
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/
Felipe C. (Guest)
on 2007-05-18 18:21
(Received via mailing list)
On 5/18/07, jim o <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.
Sebastian H. (Guest)
on 2007-05-18 18:25
(Received via mailing list)
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}"
Harry K. (Guest)
on 2007-05-18 19:34
(Received via mailing list)
On 5/18/07, jim o <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

Harry


--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
Chad P. (Guest)
on 2007-05-18 23:41
(Received via mailing list)
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.
Robert D. (Guest)
on 2007-05-18 23:46
(Received via mailing list)
On 5/18/07, Felipe C. <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
Sebastian H. (Guest)
on 2007-05-18 23:51
(Received via mailing list)
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
Lloyd L. (Guest)
on 2007-05-20 17:46
Perhaps a translation would make things clearer:

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

puts "We are going to #{destination.upcase}."
Robert D. (Guest)
on 2007-05-20 22:08
(Received via mailing list)
On 5/20/07, Lloyd L. <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
This topic is locked and can not be replied to.