Skip to content
Tags

,

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

source: http://www.techotopia.com/index.php/Ruby_Strings_-_Creation_and_Basics

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: