Alternative Syntaxes

Learn some alternative syntaxes that can be useful for creating strings and arrays.

So far, we’ve learned that strings are defined using single or double quotes, like this:

Press + to interact
"A String"
'Another String'

Arrays are defined using square brackets with a comma-separated list of objects, like so:

Press + to interact
["One", "Two", "Three"]

There are two alternative syntaxes for defining the same objects, though they’re not used very often. However, we may sometimes find them in other people’s code, so it’s helpful to know they exist.

Strings

First, Ruby has an alternative syntax for defining strings that goes like so: %[any-character]The actual string[the-same-character].

This means that a percentage character (%) followed by any other character, which also closes the whole thing, defines a string too.

For example, these definitions all mean the same thing:

Press + to interact
p "A String"
p 'A String'
p %(A String)
p %{A String}
p %|A String|

Note: Which character is used to open and close the string is a matter of style and preference. Round parentheses are good for readability, but others prefer curly braces or pipes. There’s no clear convention in the Ruby community.

Sometimes, we need to define a string that itself contains (or may contain in future versions) the same quote character that we’ve used to define the string. For example, let’s say we have a string that contains the "Name" string, including the quotes, but we’d also like to use string interpolation on the same string. We’d generally use double quotes to define the string. In that case, we’d need to escape the double quotes contained in our string.

It probably helps to look at an example. Imagine we’re working on an application that asks the user for an email address, validates the format of the given address, and then displays an error message if the format looks invalid:

The given email address "ruby@monstas" does not look like a valid email address.

Using double quotes, our code might look like this:

Press + to interact
address = "ruby@monstas"
message = "The given email address \"#{address}\" does not look like a valid email address."
puts message

Try running this code, try removing the backslashes, and try running it again. It’ll produce an unintended output because Ruby thinks the second double quote (after address) closes the first one, so it looks at #{...} outside of a string, which isn’t valid Ruby.

While the code above, including the backslash characters, is valid Ruby code, it looks a little ugly. Typing this is cumbersome, and programmers usually hate it.

So, Ruby gives us another nicer way to express the same:

Press + to interact
address = "ruby@monstas"
message = %(The given email address "#{address}" does not look like a valid email address.)
puts message

This also allows us to freely change the message later without having to change the quotes. For example, let’s say our original message looked like this:

Press + to interact
puts message = %(The given email address does not look like a valid email address.)

Suppose we wanted to use “doesn’t” instead of “does not.” We could now change it to say the following without changing the quotes:

Press + to interact
puts message = %(The given email address doesn't look like a valid email address.)

Arrays

Imagine we’re running a programming study group and we want to quickly write a piece of code that runs a raffle to randomly assign people to pairs.

Press + to interact
people = [
"Anne",
"Elizabeth",
"Erica",
"Iryna",
"Johanna",
"Juliane",
"Katja",
"Katrin",
"Maria",
"Renate",
"Sureka",
"Miriam",
"Zazie",
"Anja"
]
people.shuffle.each_slice(2) do |pair|
puts pair.join(', ')
end

This code works, but it’d be nice to define the array without having to write out all those quotes and commas. Also, if we can omit them, we can just copy and paste that list of names from somewhere else without modifying it further.

Luckily, Ruby provides us with a piece of syntax that does exactly this:

Press + to interact
people = %w(
Anne
Elizabeth
Erica
Iryna
Johanna
Juliane
Katja
Katrin
Maria
Renate
Sureka
Miriam
Zazie
Anja
)
people.shuffle.each_slice(2) do |pair|
puts pair.join(', ')
end

The w in %w(...) stands for “words,” which means that an array defined like this will always only contain strings. So, %w(1 2 3 4) results in the same array as ["1", "2", "3", "4"].