...

/

Alternative Syntaxes

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, ...