Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Begineer question

Reply
Thread Tools

Begineer question

 
 
jim o
Guest
Posts: n/a
 
      05-18-2007
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/


 
Reply With Quote
 
 
 
 
Felipe Contreras
Guest
Posts: n/a
 
      05-18-2007
On 5/18/07, jim o <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      05-18-2007
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}"


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

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      05-18-2007
On 5/18/07, jim o <(E-Mail Removed)> wrote:
>
> puts #{a}
>
> vs
> puts a
>
> Does anyone have any pointers?
>
> Thanks
> Jim
>


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/

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      05-18-2007
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}"


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.

--
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."

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-18-2007
On 5/18/07, Felipe Contreras <(E-Mail Removed)> wrote:
> On 5/18/07, jim o <(E-Mail Removed)> 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.

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
>
> --
> Felipe Contreras
>
>



--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      05-18-2007
Chad Perrin 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


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

 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      05-20-2007
Perhaps a translation would make things clearer:

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

puts "We are going to #{destination.upcase}."

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-20-2007
On 5/20/07, Lloyd Linklater <(E-Mail Removed)> 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


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Begineer help needed Jaskaranbir MCSD 0 12-28-2007 10:14 PM
begineer 10may.agn03@gmail.com C++ 21 07-10-2007 10:39 AM
stoopid begineer with a question knilges C++ 24 01-27-2006 09:37 PM
A begineer need advice =?Utf-8?B?UmFnZ29vaQ==?= Microsoft Certification 3 11-15-2005 10:02 PM
Begineer Question : Global string substitution with re peter leonard Python 0 09-22-2003 09:22 AM



Advertisments