string subs

Discussion in 'Ruby' started by shawn bright, Apr 21, 2007.

  1. shawn bright

    shawn bright Guest

    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
    shawn bright, Apr 21, 2007
    #1
    1. Advertising

  2. 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]
    Roland Crosby, Apr 21, 2007
    #2
    1. Advertising

  3. shawn bright

    shawn bright Guest

    cool, thanks very much
    sk

    On 4/21/07, Roland Crosby <> 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]
    >
    >
    shawn bright, Apr 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    547
    Ben Holness
    Oct 8, 2003
  2. Bolin

    Subs and lists

    Bolin, Nov 2, 2003, in forum: Perl
    Replies:
    6
    Views:
    554
  3. Showjumper
    Replies:
    0
    Views:
    4,373
    Showjumper
    Jun 26, 2003
  4. nakhi
    Replies:
    2
    Views:
    385
    nakhi
    Aug 22, 2003
  5. Steffen Loringer

    Useless variables/Subs checking

    Steffen Loringer, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Rob Meade
    Jan 30, 2004
Loading...

Share This Page