Alternative Syntax
Gather some alternative syntax that can be used for strings and arrays!
You have, so far, learned that Strings are defined using single or double quotes, like so:
"A String"'Another String'
And Arrays are defined using square brackets, with a comma-separated list of objects, like so:
["One", "Two", "Three"]
We’d like to quickly mention two alternative syntaxes for defining the same objects, even though they’re not used very often. However, you may sometimes find them in other people’s code, so it’s useful 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]
Meaning, that when there’s a percentage character %
followed by any other
character, which also closes the whole thing, then this defines a string, too.
For example, these definitions all mean exactly the same:
p "A String"p 'A String'p %(A String)p %{A String}p %|A String|
Which character is used to open ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy