Wednesday, February 4, 2009

Ruby - Declaring Strings using %{}

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 $end

Input: string = %{hello{world}
Output: -:1: unterminated string meets end of file


Update: 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 Content1
Output: -:1: unknown type of %string

Input: my_string5 = %aString Contenta
Output: -:1: unknown type of %string

Input: my_string6 = %[String Content[
Output: -:1: unterminated string meets end of file

3 comments:

simon said...

The other thing is it doesn't have to be a curly bracket. You can use almost anything.

irb(main):001:0> a,b,c,d = %[hey], %/yo/, %|dude|, %(etc)
=> ["hey", "yo", "dude", "etc"]

Robert Pyke said...

@Simon, updated blog post based on your comment. :D

Demon said...

Regular expression is really wonderful to parsing HTML or matching pattern. I use this a lot when i code. Actually when I learn any new langauge, first of all I first try whether it supports regex or not. I feel ezee when I found that.

http://icfun.blogspot.com/2008/04/ruby-regular-expression-handling.html

Here is about ruby regex. This was posted by me when I first learn ruby regex. So it will be helpfull for New coders.