Escape Character Table
This all of the escape sequences Ruby supports. you may not use cases of these, but memorize their format and what they do anyway. try them out in some strings to see if you can make them work.
Escape |
What it does. |
\\ |
Backslash () Backslash "\" |
\' |
Single-quote (') single quotes |
\" |
Double-quote (") double quotation marks |
\ |
Bell in ASCII Bell (BEL) ASCII code |
\ B |
Space in ASCII backspace (BS) ASCII |
\ F |
ASCII formfeed (FF) |
\ N |
ASCII linefeed (LF) |
\ R ASCII |
Carriage Return (CR) carriage return |
\ T ASCII |
Horizontal tab (Tab) tab key |
\ Uxxxx |
Character with 16-bit Hex Value xxxx (UNICODE only) |
\ Uxxxxxxxx |
Character with 32-bit Hex Value XXXXXXXX (UNICODE only) |
\ V |
ASCII vertical tab (VT) |
\ Ooo |
Character with octal value OOO oo ooo |
\ Xhh |
Character with hex value HH hexadecimal HH |
Exercise code
puts "I am 6‘2\" tall." # escape double-quote inside stringputs ‘I am 6\‘2" tall.‘ # escape single-qoute inside stringtabby_cat = "\tI‘m tabbled in."persian_cat = "I‘m splite\non a line."backslash_cat = "I‘m \\a \\ cat."fat_cat = """I‘ll do a list:\t* Cat food\t* Fishies\t* Catnip\n\t* Grass"""puts tabby_catputs persian_catputs backslash_catputs fat_cat
Result:
[[email protected] day4]$ ruby ex10.rb I am 6‘2" tall.I am 6‘2" tall. I‘m tabbled in.I‘m spliteon a line.I‘m \a \ cat.I‘ll do a list: * Cat food * Fishies * Catnip * Grass
Use three single quotes ('') instead of three double quotes (""")
read_list = ‘‘‘this is new year\‘s read list(just maybe, not very sure, I guess...)\t* Camille\t* Madame Bovary\t* The Red and the Black\t* True Blood‘‘‘
The result is as follows:
this is new year‘s read list(just maybe, not very sure, I guess...)\t* Camille\t* Madame Bovary\t* The Red and the Black\t* True Blood
It is displayed as is, which is also the difference between double quotation marks and single quotation marks used in strings.
Lrthw exercise 10