# String subs

**URL:** <https://www.ruby-forum.com/t/string-subs/92634>\
**Category:** Ruby\
**Created:** [April 21, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/string-subs/92634 "2007-04-21T23:42:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [April 21, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/string-subs/92634/1 "2007-04-21T23:42:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [April 21, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/string-subs/92634/2 "2007-04-21T23:45:00Z")

</div>

On Apr 21, 2007, at 5:40 PM, shawn bright wrote:

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

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [April 22, 2007, 12:39am UTC](https://www.ruby-forum.com/t/string-subs/92634/3 "2007-04-22T00:39:05Z")

</div>

cool, thanks very much  
sk
