string subs

S

shawn bright

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
 
R

Roland Crosby

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]
 
S

shawn bright

cool, thanks very much
sk

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]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top