Forum: Ruby string subs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-04-21 23:42
(Received via mailing list)
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
F56bed1a81c2216347b9f297fbcbab1b?d=identicon&s=25 Roland Crosby (Guest)
on 2007-04-21 23:45
(Received via mailing list)
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]
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-04-22 00:39
(Received via mailing list)
cool, thanks very much
sk
This topic is locked and can not be replied to.