U:RDoc::TopLevel[ i I"syntax/literals.rdoc:EFcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[S:RDoc::Markup::Heading:
leveli: textI"
Literals;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"LLiterals create objects you can use in your program. Literals include:;T@
o:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o;
;[I"Booleans and nil;To;;0;[o;
;[I"Numbers;To;;0;[o;
;[I"Strings;To;;0;[o;
;[I"Symbols;To;;0;[o;
;[I"Arrays;To;;0;[o;
;[I"Hashes;To;;0;[o;
;[I"Ranges;To;;0;[o;
;[I"Regular Expressions;To;;0;[o;
;[I"
Procs;T@
S; ;
i;I"Booleans and nil;T@
o;
;[I"S+nil+ and +false+ are both false values. +nil+ is sometimes used to indicate ;TI"Q"no value" or "unknown" but evaluates to +false+ in conditional expressions.;T@
o;
;[I"Q+true+ is a true value. All objects except +nil+ and +false+ evaluate to a ;TI"+true value in conditional expressions.;T@
o;
;[I"P(There are also the constants +TRUE+, +FALSE+ and +NIL+, but the lowercase ;TI""literal forms are preferred.);T@
S; ;
i;I"Numbers;T@
o;
;[I"3You can write integers of any size as follows:;T@
o:RDoc::Markup::Verbatim;[I"
1234
;TI"1_234
;T:@format0o;
;[I"NThese numbers have the same value, 1,234. The underscore may be used to ;TI"Renhance readability for humans. You may place an underscore anywhere in the ;TI"number.;T@
o;
;[I"6Floating point numbers may be written as follows:;T@
o;;[I"12.34
;TI"
1234e-2
;TI"
1.234E1
;T;0o;
;[I"TThese numbers have the same value, 12.34. You may use underscores in floating ;TI"point numbers as well.;T@
o;
;[
I"RYou can use a special prefix to write numbers in decimal, hexadecimal, octal ;TI"Nor binary formats. For decimal numbers use a prefix of 0d, for ;TI"Nhexadecimal numbers use a prefix of 0x, for octal numbers use a ;TI"Mprefix of 0 or 0o, for binary numbers use a prefix of ;TI"P0b. The alphabetic component of the number is not case-sensitive.;T@
o;
;[I"Examples:;T@
o;;[I"0d170
;TI"0D170
;TI"
;TI"
0xaa
;TI"
0xAa
;TI"
0xAA
;TI"
0Xaa
;TI"
0XAa
;TI"
0XaA
;TI"
;TI"
0252
;TI"0o252
;TI"0O252
;TI"
;TI"0b10101010
;TI"0B10101010
;T;0o;
;[I"SAll these numbers have the same decimal value, 170. Like integers and floats ;TI"/you may use an underscore for readability.;T@
S; ;
i;I"Strings;T@
o;
;[I"@The most common way of writing strings is using ":;T@
o;;[I""This is a string."
;T;0o;
;[I"'The string may be many lines long.;T@
o;
;[I"-Any internal " must be escaped:;T@
o;;[I"C"This string has a quote: \". As you can see, it is escaped"
;T;0o;
;[I"KDouble-quote strings allow escaped characters such as \n for ;TI"Knewline, \t for tab, etc. The full list of supported escape ;TI"sequences are as follows:;T@
o;;[I"*\a bell, ASCII 07h (BEL)
;TI".\b backspace, ASCII 08h (BS)
;TI"4\t horizontal tab, ASCII 09h (TAB)
;TI"8\n newline (line feed), ASCII 0Ah (LF)
;TI"1\v vertical tab, ASCII 0Bh (VT)
;TI".\f form feed, ASCII 0Ch (FF)
;TI"4\r carriage return, ASCII 0Dh (CR)
;TI",\e escape, ASCII 1Bh (ESC)
;TI"+\s space, ASCII 20h (SPC)
;TI"!\\ backslash, \
;TI"M\nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
;TI"^\xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
;TI"`\unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
;TI"b\u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
;TI"O\cx or \C-x control character, where x is an ASCII printable character
;TI"L\M-x meta character, where x is an ASCII printable character
;TI"T\M-\C-x meta control character, where x is an ASCII printable character
;TI""\M-\cx same as above
;TI""\c\M-x same as above
;TI",\c? or \C-? delete, ASCII 7Fh (DEL)
;T;0o;
;[I"EAny other character following a backslash is interpreted as the ;TI"character itself.;T@
o;
;[I"DDouble-quote strings allow interpolation of other values using ;TI"#{...}:;T@
o;;[I"%"One plus one is two: #{1 + 1}"
;T;0o;
;[I"TAny expression may be placed inside the interpolated section, but it's best to ;TI"/keep the expression small for readability.;T@
o;
;[I"JInterpolation may be disabled by escaping the "#" character or using ;TI"single-quote strings:;T@
o;;[I" '#{1 + 1}' #=> "\#{1 + 1}"
;T;0o;
;[I"TIn addition to disabling interpolation, single-quoted strings also disable all ;TI"Nescape sequences except for the single-quote (\') and backslash ;TI"(\\\\).;T@
o;
;[I"2You may also create strings using %:;T@
o;;[I"+%(1 + 1 is #{1 + 1}) #=> "1 + 1 is 2"
;T;0o;
;[ I"RThere are two different types of % strings %q(...) behaves ;TI"Plike a single-quote string (no interpolation or character escaping), while ;TI"R%Q behaves as a double-quote string. See Percent Strings below for ;TI"6more discussion of the syntax of percent strings.;T@
o;
;[I"PAdjacent string literals are automatically concatenated by the interpreter:;T@
o;;[I"5"con" "cat" "en" "at" "ion" #=> "concatenation"
;TI""This string contains "\
;TI"I"no newlines." #=> "This string contains no newlines."
;T;0o;
;[I"RAny combination of adjacent single-quote, double-quote, percent strings will ;TI"=be concatenated as long as a percent-string is not last.;T@
o;;[I"%q{a} 'b' "c" #=> "abc"
;TI";"a" 'b' %q{c} #=> NameError: uninitialized constant q
;T;0o;
;[ I"DThere is also a character literal notation to represent single ;TI"Echaracter strings, which syntax is a question mark (?) ;TI"Kfollowed by a single character or escape sequence that corresponds to ;TI"/a single codepoint in the script encoding:;T@
o;;[I"?a #=> "a"
;TI"?abc #=> SyntaxError
;TI"?\n #=> "\n"
;TI"?\s #=> " "
;TI"?\\ #=> "\\"
;TI"?\u{41} #=> "A"
;TI"?\C-a #=> "\x01"
;TI"?\M-a #=> "\xE1"
;TI"?\M-\C-a #=> "\x81"
;TI"(?\C-\M-a #=> "\x81", same as above
;TI"?あ #=> "あ"
;T;0S; ;
i;I"Here Documents;T@
o;
;[I"OIf you are writing a large block of text you may use a "here document" or ;TI""heredoc":;T@
o;;[
I"!expected_result = << and ends with the ;TI"Rnext line that starts with HEREDOC. The result includes the ending ;TI"
newline.;T@
o;
;[I"RYou may use any identifier with a heredoc, but all-uppercase identifiers are ;TI"typically used.;T@
o;
;[I"OYou may indent the ending identifier if you place a "-" after <<:;T@
o;;[
I"- expected_result = <<-INDENTED_HEREDOC
;TI"2This would contain specially formatted text.
;TI"
;TI" That might span many lines
;TI" INDENTED_HEREDOC
;T;0o;
;[I"PNote that the while the closing identifier may be indented, the content is ;TI"Talways treated as if it is flush left. If you indent the content those spaces ;TI"will appear in the output.;T@
o;
;[I"UTo have indented content as well as an indented closing identifier, you can use ;TI"Oa "squiggly" heredoc, which uses a "~" instead of a "-" after <<:;T@
o;;[
I"+expected_result = <<~SQUIGGLY_HEREDOC
;TI"4 This would contain specially formatted text.
;TI"
;TI"" That might span many lines
;TI"SQUIGGLY_HEREDOC
;T;0o;
;[ I"RThe indentation of the least-indented line will be removed from each line of ;TI"Uthe content. Note that empty lines and lines consisting solely of literal tabs ;TI"Qand spaces will be ignored for the purposes of determining indentation, but ;TI"Gescaped tabs and spaces are considered non-indentation characters.;T@
o;
;[I"MA heredoc allows interpolation and escaped characters. You may disable ;TI"Rinterpolation and escaping by surrounding the opening identifier with single ;TI"quotes:;T@
o;;[
I"%expected_result = <<-'EXPECTED'
;TI"One plus one is #{1 + 1}
;TI"EXPECTED
;TI"
;TI"?p expected_result # prints: "One plus one is \#{1 + 1}\n"
;T;0o;
;[I"TThe identifier may also be surrounded with double quotes (which is the same as ;TI"Mno quotes) or with backticks. When surrounded by backticks the HEREDOC ;TI"behaves like Kernel#`:;T@
o;;[I"puts <<-`HEREDOC`
;TI"cat #{__FILE__}
;TI"
HEREDOC
;T;0o;
;[I"ITo call a method on a heredoc place it after the opening identifier:;T@
o;;[I")expected_result = <<-EXPECTED.chomp
;TI"One plus one is #{1 + 1}
;TI"EXPECTED
;T;0o;
;[I"SYou may open multiple heredocs on the same line, but this can be difficult to ;TI"
read:;T@
o;;[
I"puts(<<-ONE, <<-TWO)
;TI"content for heredoc one
;TI" ONE
;TI"content for heredoc two
;TI" TWO
;T;0S; ;
i;I"Symbols;T@
o;
;[I"RA Symbol represents a name inside the ruby interpreter. See Symbol for more ;TI"Gdetails on what symbols are and when ruby creates them internally.;T@
o;
;[I"CYou may reference a symbol using a colon: :my_symbol.;T@
o;
;[I"2You may also create symbols by interpolation:;T@
o;;[I":"my_symbol1"
;TI":"my_symbol#{1 + 1}"
;T;0o;
;[I"GLike strings, a single-quote may be used to disable interpolation:;T@
o;;[I"4:'my_symbol#{1 + 1}' #=> :"my_symbol\#{1 + 1}"
;T;0o;
;[I"QWhen creating a Hash, there is a special syntax for referencing a Symbol as ;TI"
well.;T@
S; ;
i;I"Arrays;T@
o;
;[I"MAn array is created using the objects between [ and ]:;T@
o;;[I"[1, 2, 3]
;T;0o;
;[I"0You may place expressions inside the array:;T@
o;;[I"[1, 1 + 1, 1 + 2]
;TI"[1, [1 + 1, [1 + 2]]]
;T;0o;
;[I"9See Array for the methods you may use with an array.;T@
S; ;
i;I"Hashes;T@
o;
;[I"OA hash is created using key-value pairs between { and }:;T@
o;;[I"{ "a" => 1, "b" => 2 }
;T;0o;
;[I".Both the key and value may be any object.;T@
o;
;[I"GYou can create a hash using symbol keys with the following syntax:;T@
o;;[I"{ a: 1, b: 2 }
;T;0o;
;[I"AThis same syntax is used for keyword arguments for a method.;T@
o;
;[I"5Like Symbol literals, you can quote symbol keys.;T@
o;;[I"#{ "a 1": 1, "b #{1 + 1}": 2 }
;T;0o;
;[I"is equal to;T@
o;;[I""{ :"a 1" => 1, :"b 2" => 2 }
;T;0o;
;[I"6See Hash for the methods you may use with a hash.;T@
S; ;
i;I"Ranges;T@
o;
;[I"QA range represents an interval of values. The range may include or exclude ;TI"its ending value.;T@
o;;[I")(1..2) # includes its ending value
;TI")(1...2) # excludes its ending value
;T;0o;
;[I"TYou may create a range of any object. See the Range documentation for details ;TI"*on the methods you need to implement.;T@
S; ;
i;I"Regular Expressions;T@
o;
;[I"/A regular expression is created using "/":;T@
o;;[I"/my regular expression/
;T;0o;
;[I"OThe regular expression may be followed by flags which adjust the matching ;TI"Tbehavior of the regular expression. The "i" flag makes the regular expression ;TI"case-insensitive:;T@
o;;[I"/my regular expression/i
;T;0o;
;[I"MInterpolation may be used inside regular expressions along with escaped ;TI"Pcharacters. Note that a regular expression may require additional escaped ;TI"characters than a string.;T@
o;
;[I"GSee Regexp for a description of the syntax of regular expressions.;T@
S; ;
i;I"
Procs;T@
o;
;[I",A proc can be created with ->:;T@
o;;[I"-> { 1 + 1 }
;T;0o;
;[I"=Calling the above proc will give a result of 2.;T@
o;
;[I"7You can require arguments for the proc as follows:;T@
o;;[I"->(v) { 1 + v }
;T;0o;
;[I",This proc will add one to its argument.;T@
S; ;
i;I"Percent Strings;T@
o;
;[I"OBesides %(...) which creates a String, the % may create ;TI"Iother types of object. As with strings, an uppercase letter allows ;TI"Qinterpolation and escaped characters while a lowercase letter disables them.;T@
o;
;[I"4These are the types of percent strings in ruby:;T@
o;;: NOTE;[o;;[I"%i ;T;[o;
;[I"Array of Symbols;To;;[I"%q ;T;[o;
;[I"String;To;;[I"%r ;T;[o;
;[I"Regular Expression;To;;[I"%s ;T;[o;
;[I"Symbol;To;;[I"%w ;T;[o;
;[I"Array of Strings;To;;[I"%x ;T;[o;
;[I"'Backtick (capture subshell result);T@
o;
;[I"RFor the two array forms of percent string, if you wish to include a space in ;TI"Gone of the array entries you must escape it with a "\\" character:;T@
o;;[I"%w[one one-hundred\ one]
;TI"$#=> ["one", "one-hundred one"]
;T;0o;
;[I"SIf you are using "(", "[", "{", "<" you must close it with ")", "]", "}", ">" ;TI"Srespectively. You may use most other non-alphanumeric characters for percent ;TI"2string delimiters such as "%", "|", "^", etc.;T:
@file@:0@omit_headings_from_table_of_contents_below0