Ruby/String/String Quotation
Материал из Wiki.crossplatform.ru
Версия от 17:56, 13 сентября 2010; ViGOur (Обсуждение | вклад)
Another way to build up a long string literal is by using a here document
x = <<END_MY_STRING_PLEASE This is the string And a second line END_MY_STRING_PLEASE puts x
double quotes interpret escaped characters and single quotes preserve them.
lear = "K\nA T\nW" puts lear # And here"s what you get with single quotes (preserves \n in context): lear = "K\nA\nW" puts lear
General Delimited Strings
# Create strings is with general delimited strings # Strings are all preceded by a % and then followed by a matched pair of delimiter characters, such as !, {, or [ (must be nonalphanumeric). # The string is embedded between the delimiters. comedy = %!this is a test! history = %[this is a test] tragedy = %(this is a test)
Use pairs of delimiters, such as { and } or < and > when quoting
puts %Q{Yes}
Use %Q! to create a string
puts %Q!Yes, that was Cary Grant.!
Use %q to represent single quote
puts %q/Are you sure?/
Using quotation marks is only viable for a single line, but if you want to span multiple lines
x = %q{This is a test of the multi line capabilities} puts x
you can omit the Q altogether
puts %/No/
you don"t even have to use single or double quotes to create a string
# Ruby can add them if you use the %q (single quotes) or %Q (double quotes) syntax. puts %Q/Yes, that was Cary Grant./