Hi guys/gals,
In Ruby, you can declare a string like follows:
string = "a string"You can also declare a string with:
string = %{Another string}The cool thing about the %{} declaration is that it is context aware. That is to say, it knows the depth of the
curly stack. It knows when the next curly will end a start curly within the string and when the next curly will end the curly which started the string.
The following example demonstrates this:
my_string =
%{1st line, it is fairly normal.
2nd line , it contains "quotation marks".
3rd line, it contains one set of curlies, { a set }.
4th, it contains two sets of curlies, { first set }, { second set }.
5th line, this marks the end of my_string.}The above works because the sets of curlies start and end within the string.
The following examples show what will happen if you
incorrectly use %{}:
Input:
string = %{hello}world}Output:
-:1: syntax error, unexpected tIDENTIFIER, expecting $endInput:
string = %{hello{world}Output:
-:1: unterminated string meets end of fileUpdate: As mentioned by
Simon, you can use any non letter/number character to mark the start and end of a string. The character after the
% marks the start of the String, if the character has an end pair character, the way in which you declare the String will work as I have described for %{}. If the character does not have an end pair character, the next use of the character will mark the end of the String.
Pair characters include:
%{}, %[], %(), %<>The following shows different examples of character combinations you
can use to start and end a string:
Start:
%{ end:
}Start:
%} end:
}Start:
%[ end:
]Start:
%] end:
]Start:
%! end:
!The following shows an example of a character combination you
cannot use:
Start:
%{ end:
{The following are examples of
correct String declarations:
my_string1 =
%[My string, I have some other brackets in my String [a, b, c], yay
for context aware Strings.]my_string2 = %$MyString which is ended by a dollar sign$my_string3 = %<Yet another example of a String which is closed by it's sensible pair character>my_string4 = %%Yes, even the percent sign can be used%The following are examples of
invalid/incorrect String declarations:
Input:
my_string4 = %1String Content1Output:
-:1: unknown type of %stringInput:
my_string5 = %aString ContentaOutput:
-:1: unknown type of %stringInput:
my_string6 = %[String Content[Output:
-:1: unterminated string meets end of file