Skip to content


Delimiting string in ruby

March 5, 2013

Ruby allows you to define any character you want as a string delimiter simply by prefixing the desired character with a %. For example, we could use the ampersand to delimit our string:

myString = %&This is my String&

This enables us to avoid quotes and double quotes embedded in a string being interpreted as delimiters:

myString = %&This is "my" String&
puts myString
This is "my" String

It is also possible to define delimiter pairs such as parentheses, braces or square brackets:

myString = %(This is my String)
myString = %[This is my String]
myString = %{This is my String}

Ruby also provides a few special delimited strings. %Q is the equivalent of double quote delimiters and %q is the equivalent of single quotes. %x is the equivalent of back-quote (`) delimited



From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: