155

In Python, this idiom for string formatting is quite common

s = "hello, %s. Where is %s?" % ("John","Mary")

What is the equivalent in Ruby?

Jordan Dea-Mattson
  • 5,791
  • 5
  • 38
  • 53
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 3
    There's no string concatenation, only string formatting. And google will answer this faster than even SO ever could... it took me about 20 seconds to find out you can do exactly the same in Ruby. –  Aug 24 '10 at 07:39
  • You're not asking for concatenation, your asking for formatting/substitution. You should change your question. By the way, in ruby, concatenation is performed with + or << operators. – David Aug 24 '10 at 07:49

4 Answers4

269

The easiest way is string interpolation. You can inject little pieces of Ruby code directly into your strings.

name1 = "John"
name2 = "Mary"
"hello, #{name1}.  Where is #{name2}?"

You can also do format strings in Ruby.

"hello, %s.  Where is %s?" % ["John", "Mary"]

Remember to use square brackets there. Ruby doesn't have tuples, just arrays, and those use square brackets.

siegy22
  • 4,295
  • 3
  • 25
  • 43
AboutRuby
  • 7,936
  • 2
  • 27
  • 20
  • 1
    I would definitively use the first one, it looks more readable to me – David Aug 24 '10 at 07:51
  • 1
    The first one won't work, the #{} looks for a variable, so in this case it'd be printing the John variable, not the string "John". The second one looks correct. – Jason Noble Aug 24 '10 at 14:59
  • 12
    You should also be careful to **always use double quotes** as `'#{name1}'` is not the same as `"#{name1}"`. – yurisich Dec 06 '12 at 16:21
  • @Droogans: Thanks for the tip. Could you elaborate a bit on this? – kasia May 05 '13 at 13:06
  • 4
    String interpolation does not work in single quotes, you must use double quotes. For example:`'#{"abc"}' # => "\#{\"abc\"}"`, but what you want is `"#{"abc"}" # => "abc"` – bschlueter Feb 04 '14 at 17:00
  • The format string method also allows you to tweak the format (appropriately enough): `"%2d: %05d" % [9, 130] => " 9: 00130"` – alxndr Feb 17 '14 at 19:56
  • 3
    The first way is not an equivalent - the template can't be passed around as a value. – Ondra Žižka Dec 18 '17 at 18:20
  • Doing something like `"%-20s %s" % ["John", "Mary"]` is a great reason to know about the second one! Thank you :) – mbigras Jan 24 '18 at 16:28
55

In Ruby > 1.9 you can do this:

s =  'hello, %{name1}. Where is %{name2}?' % { name1: 'John', name2: 'Mary' }

See the docs

siegy22
  • 4,295
  • 3
  • 25
  • 43
toong
  • 1,360
  • 12
  • 18
  • I edited in the *Idomatic Ruby* equivalent there - but now I notice the accepted answer already does that - removed the 'edit' again :-/ – toong Aug 07 '14 at 11:16
  • When using a hash with the string % operator, if the key symbol referenced in the format string isn't present, you'll get a KeyError. – user1164178 Feb 11 '15 at 00:52
22

Almost the same way:

"hello, %s. Where is %s?" % ["John","Mary"]
# => "hello, John. Where is Mary?"
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
  • In Ruby, do the square brackets mean a tuple? I thought square brackets are lists... – TIMEX Aug 24 '10 at 07:39
  • @TIMEX: This question will help: http://stackoverflow.com/questions/525957/tuples-in-ruby – Manoj Govindan Aug 24 '10 at 07:40
  • 3
    Ruby doesn't have tuples (at least not forged into the language). Yeah, it's an array ("list" in Python should really be called arrays...). –  Aug 24 '10 at 07:41
10

Actually almost the same

s = "hello, %s. Where is %s?" % ["John","Mary"]
phadej
  • 11,947
  • 41
  • 78