Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > string subs

Reply
Thread Tools

string subs

 
 
shawn bright
Guest
Posts: n/a
 
      04-21-2007
lo there all,

i have found how i can put a variable inside a string without the #{} stuff
using %s and so on. But i don't know how to do more than one.

for example

x = 5
y = 10

puts "i have %s foo and %s bar" % don't know what goes here

i know, quite a beginner question, would appreciate any help

thanks
sk

 
Reply With Quote
 
 
 
 
Roland Crosby
Guest
Posts: n/a
 
      04-21-2007
On Apr 21, 2007, at 5:40 PM, shawn bright wrote:

> lo there all,
>
> i have found how i can put a variable inside a string without the #
> {} stuff
> using %s and so on. But i don't know how to do more than one.
>
> for example
>
> x = 5
> y = 10
>
> puts "i have %s foo and %s bar" % don't know what goes here
>
> i know, quite a beginner question, would appreciate any help
>
> thanks
> sk
>


From ri:

--------------------------------------------------------------- String#%
str % arg => new_str
------------------------------------------------------------------------
Format---Uses _str_ as a format specification, and returns the
result of applying it to _arg_. If the format specification
contains more than one substitution, then _arg_ must be an +Array+
containing the values to be substituted. See +Kernel::sprintf+ for
details of the format string.

"%05d" % 123 #=> "00123"
"%-5s: %08x" % [ "ID", self.id ] #=> "ID : 200e14d6"

So, it'd be:

puts "i have %s foo and %s bar" % [foo, bar]

 
Reply With Quote
 
 
 
 
shawn bright
Guest
Posts: n/a
 
      04-21-2007
cool, thanks very much
sk

On 4/21/07, Roland Crosby <(E-Mail Removed)> wrote:
> On Apr 21, 2007, at 5:40 PM, shawn bright wrote:
>
> > lo there all,
> >
> > i have found how i can put a variable inside a string without the #
> > {} stuff
> > using %s and so on. But i don't know how to do more than one.
> >
> > for example
> >
> > x = 5
> > y = 10
> >
> > puts "i have %s foo and %s bar" % don't know what goes here
> >
> > i know, quite a beginner question, would appreciate any help
> >
> > thanks
> > sk
> >

>
> From ri:
>
> --------------------------------------------------------------- String#%
> str % arg => new_str
> ------------------------------------------------------------------------
> Format---Uses _str_ as a format specification, and returns the
> result of applying it to _arg_. If the format specification
> contains more than one substitution, then _arg_ must be an +Array+
> containing the values to be substituted. See +Kernel::sprintf+ for
> details of the format string.
>
> "%05d" % 123 #=> "00123"
> "%-5s: %08x" % [ "ID", self.id ] #=> "ID : 200e14d6"
>
> So, it'd be:
>
> puts "i have %s foo and %s bar" % [foo, bar]
>
>


 
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
Useless variables/Subs checking Steffen Loringer ASP .Net 1 01-30-2004 11:10 AM
Subs and lists Bolin Perl 6 11-03-2003 11:01 PM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM
dataset can not be shared across subs nakhi ASP .Net 2 08-22-2003 01:56 AM
Help w/ LoadViewState and SaveViewState subs Showjumper ASP .Net 0 06-26-2003 01:38 PM



Advertisments