2012-12-16 21:26:15 -05:00
|
|
|
= Literals
|
|
|
|
|
|
|
|
Literals create objects you can use in your program. Literals include:
|
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
* {Boolean and Nil Literals}[#label-Boolean+and+Nil+Literals]
|
|
|
|
* {Number Literals}[#label-Number+Literals]
|
|
|
|
|
|
|
|
* {Integer Literals}[#label-Integer+Literals]
|
|
|
|
* {Float Literals}[#label-Float+Literals]
|
|
|
|
* {Rational Literals}[#label-Rational+Literals]
|
|
|
|
* {Complex Literals}[#label-Complex+Literals]
|
|
|
|
|
|
|
|
* {String Literals}[#label-String+Literals]
|
|
|
|
* {Here Document Literals}[#label-Here+Document+Literals]
|
|
|
|
* {Symbol Literals}[#label-Symbol+Literals]
|
|
|
|
* {Array Literals}[#label-Array+Literals]
|
|
|
|
* {Hash Literals}[#label-Hash+Literals]
|
|
|
|
* {Range Literals}[#label-Range+Literals]
|
|
|
|
* {Regexp Literals}[#label-Regexp+Literals]
|
|
|
|
* {Lambda Proc Literals}[#label-Lambda+Proc+Literals]
|
|
|
|
* {Percent Literals}[#label-Percent+Literals]
|
|
|
|
|
|
|
|
* {%q: Non-Interpolable String Literals}[#label-25q-3A+Non-Interpolable+String+Literals]
|
|
|
|
* {% and %Q: Interpolable String Literals}[#label-25+and+-25Q-3A+Interpolable+String+Literals]
|
|
|
|
* {%w and %W: String-Array Literals}[#label-25w+and+-25W-3A+String-Array+Literals]
|
|
|
|
* {%i and %I: Symbol-Array Literals}[#label-25i+and+-25I-3A+Symbol-Array+Literals]
|
|
|
|
* {%r: Regexp Literals}[#label-25r-3A+Regexp+Literals]
|
|
|
|
* {%s: Symbol Literals}[#label-25s-3A+Symbol+Literals]
|
|
|
|
* {%x: Backtick Literals}[#label-25x-3A+Backtick+Literals]
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== Boolean and Nil Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
+nil+ and +false+ are both false values. +nil+ is sometimes used to indicate
|
|
|
|
"no value" or "unknown" but evaluates to +false+ in conditional expressions.
|
|
|
|
|
|
|
|
+true+ is a true value. All objects except +nil+ and +false+ evaluate to a
|
|
|
|
true value in conditional expressions.
|
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
== Number Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-11-27 14:07:37 -05:00
|
|
|
=== \Integer Literals
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
You can write integers of any size as follows:
|
|
|
|
|
|
|
|
1234
|
|
|
|
1_234
|
|
|
|
|
|
|
|
These numbers have the same value, 1,234. The underscore may be used to
|
|
|
|
enhance readability for humans. You may place an underscore anywhere in the
|
|
|
|
number.
|
|
|
|
|
2012-12-19 02:30:58 -05:00
|
|
|
You can use a special prefix to write numbers in decimal, hexadecimal, octal
|
|
|
|
or binary formats. For decimal numbers use a prefix of <tt>0d</tt>, for
|
2012-12-16 21:26:15 -05:00
|
|
|
hexadecimal numbers use a prefix of <tt>0x</tt>, for octal numbers use a
|
2012-12-17 21:48:35 -05:00
|
|
|
prefix of <tt>0</tt> or <tt>0o</tt>, for binary numbers use a prefix of
|
|
|
|
<tt>0b</tt>. The alphabetic component of the number is not case-sensitive.
|
|
|
|
|
|
|
|
Examples:
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2012-12-19 02:30:58 -05:00
|
|
|
0d170
|
|
|
|
0D170
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
0xaa
|
|
|
|
0xAa
|
|
|
|
0xAA
|
|
|
|
0Xaa
|
|
|
|
0XAa
|
|
|
|
0XaA
|
|
|
|
|
|
|
|
0252
|
2012-12-17 21:48:35 -05:00
|
|
|
0o252
|
|
|
|
0O252
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
0b10101010
|
|
|
|
0B10101010
|
|
|
|
|
|
|
|
All these numbers have the same decimal value, 170. Like integers and floats
|
|
|
|
you may use an underscore for readability.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
=== \Float Literals
|
2021-11-27 14:07:37 -05:00
|
|
|
|
|
|
|
Floating-point numbers may be written as follows:
|
|
|
|
|
|
|
|
12.34
|
|
|
|
1234e-2
|
|
|
|
1.234E1
|
|
|
|
|
|
|
|
These numbers have the same value, 12.34. You may use underscores in floating
|
|
|
|
point numbers as well.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
=== \Rational Literals
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2022-04-24 20:41:26 -04:00
|
|
|
You can write a Rational literal using a special suffix, <tt>'r'</tt>.
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2022-04-24 20:41:26 -04:00
|
|
|
Examples:
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2022-04-24 20:41:26 -04:00
|
|
|
1r # => (1/1)
|
|
|
|
2/3r # => (2/3) # With denominator.
|
|
|
|
-1r # => (-1/1) # With signs.
|
|
|
|
-2/3r # => (-2/3)
|
|
|
|
2/-3r # => (-2/3)
|
|
|
|
-2/-3r # => (2/3)
|
|
|
|
+1/+3r # => (1/3)
|
|
|
|
1.2r # => (6/5) # With fractional part.
|
|
|
|
1_1/2_1r # => (11/21) # With embedded underscores.
|
|
|
|
2/4r # => (1/2) # Automatically reduced.
|
|
|
|
|
|
|
|
Syntax:
|
|
|
|
|
|
|
|
<rational-literal> = <numerator> [ '/' <denominator> ] 'r'
|
|
|
|
<numerator> = [ <sign> ] <digits> [ <fractional-part> ]
|
|
|
|
<fractional-part> = '.' <digits>
|
|
|
|
<denominator> = [ sign ] <digits>
|
|
|
|
<sign> = '-' | '+'
|
|
|
|
<digits> = <digit> { <digit> | '_' <digit> }
|
|
|
|
<digit> = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|
|
|
|
|
|
|
|
Note this, which is parsed as \Float numerator <tt>1.2</tt>
|
|
|
|
divided by \Rational denominator <tt>3r</tt>,
|
|
|
|
resulting in a \Float:
|
|
|
|
|
|
|
|
1.2/3r # => 0.39999999999999997
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
=== \Complex Literals
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
You can write a Complex number as follows (suffixed +i+):
|
2018-11-16 20:21:18 -05:00
|
|
|
|
|
|
|
1i #=> (0+1i)
|
|
|
|
1i * 1i #=> (-1+0i)
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
Also \Rational numbers may be imaginary numbers.
|
2018-11-16 20:21:18 -05:00
|
|
|
|
|
|
|
12.3ri #=> (0+(123/10)*i)
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
+i+ must be placed after +r+; the opposite is not allowed.
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
12.3ir #=> Syntax error
|
2018-11-16 20:21:18 -05:00
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
== Strings
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
=== \String Literals
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
The most common way of writing strings is using <tt>"</tt>:
|
|
|
|
|
2013-01-16 16:54:31 -05:00
|
|
|
"This is a string."
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
The string may be many lines long.
|
|
|
|
|
|
|
|
Any internal <tt>"</tt> must be escaped:
|
|
|
|
|
|
|
|
"This string has a quote: \". As you can see, it is escaped"
|
|
|
|
|
2015-09-16 02:03:18 -04:00
|
|
|
Double-quote strings allow escaped characters such as <tt>\n</tt> for
|
|
|
|
newline, <tt>\t</tt> for tab, etc. The full list of supported escape
|
|
|
|
sequences are as follows:
|
|
|
|
|
|
|
|
\a bell, ASCII 07h (BEL)
|
|
|
|
\b backspace, ASCII 08h (BS)
|
|
|
|
\t horizontal tab, ASCII 09h (TAB)
|
|
|
|
\n newline (line feed), ASCII 0Ah (LF)
|
|
|
|
\v vertical tab, ASCII 0Bh (VT)
|
|
|
|
\f form feed, ASCII 0Ch (FF)
|
|
|
|
\r carriage return, ASCII 0Dh (CR)
|
|
|
|
\e escape, ASCII 1Bh (ESC)
|
|
|
|
\s space, ASCII 20h (SPC)
|
|
|
|
\\ backslash, \
|
|
|
|
\nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
|
|
|
|
\xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
|
|
|
|
\unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
|
|
|
|
\u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
|
|
|
|
\cx or \C-x control character, where x is an ASCII printable character
|
|
|
|
\M-x meta character, where x is an ASCII printable character
|
|
|
|
\M-\C-x meta control character, where x is an ASCII printable character
|
2015-09-16 02:21:13 -04:00
|
|
|
\M-\cx same as above
|
|
|
|
\c\M-x same as above
|
|
|
|
\c? or \C-? delete, ASCII 7Fh (DEL)
|
2015-09-16 02:03:18 -04:00
|
|
|
|
2017-02-03 18:34:48 -05:00
|
|
|
Any other character following a backslash is interpreted as the
|
2015-09-16 02:03:18 -04:00
|
|
|
character itself.
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
Double-quote strings allow interpolation of other values using
|
|
|
|
<tt>#{...}</tt>:
|
|
|
|
|
|
|
|
"One plus one is two: #{1 + 1}"
|
|
|
|
|
|
|
|
Any expression may be placed inside the interpolated section, but it's best to
|
|
|
|
keep the expression small for readability.
|
|
|
|
|
2020-09-29 02:28:09 -04:00
|
|
|
You can also use <tt>#@foo</tt>, <tt>#@@foo</tt> and <tt>#$foo</tt> as a
|
2020-10-11 23:40:57 -04:00
|
|
|
shorthand for, respectively, <tt>#{ @foo }</tt>, <tt>#{ @@foo }</tt> and
|
2020-09-29 02:28:09 -04:00
|
|
|
<tt>#{ $foo }</tt>.
|
2020-09-29 01:39:21 -04:00
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
Interpolation may be disabled by escaping the "#" character or using
|
|
|
|
single-quote strings:
|
|
|
|
|
|
|
|
'#{1 + 1}' #=> "\#{1 + 1}"
|
|
|
|
|
|
|
|
In addition to disabling interpolation, single-quoted strings also disable all
|
2014-03-13 23:09:17 -04:00
|
|
|
escape sequences except for the single-quote (<tt>\'</tt>) and backslash
|
|
|
|
(<tt>\\\\</tt>).
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2013-08-12 19:54:38 -04:00
|
|
|
Adjacent string literals are automatically concatenated by the interpreter:
|
|
|
|
|
|
|
|
"con" "cat" "en" "at" "ion" #=> "concatenation"
|
|
|
|
"This string contains "\
|
|
|
|
"no newlines." #=> "This string contains no newlines."
|
|
|
|
|
|
|
|
Any combination of adjacent single-quote, double-quote, percent strings will
|
|
|
|
be concatenated as long as a percent-string is not last.
|
|
|
|
|
|
|
|
%q{a} 'b' "c" #=> "abc"
|
|
|
|
"a" 'b' %q{c} #=> NameError: uninitialized constant q
|
|
|
|
|
2015-09-16 02:16:15 -04:00
|
|
|
There is also a character literal notation to represent single
|
|
|
|
character strings, which syntax is a question mark (<tt>?</tt>)
|
|
|
|
followed by a single character or escape sequence that corresponds to
|
|
|
|
a single codepoint in the script encoding:
|
|
|
|
|
|
|
|
?a #=> "a"
|
|
|
|
?abc #=> SyntaxError
|
|
|
|
?\n #=> "\n"
|
|
|
|
?\s #=> " "
|
|
|
|
?\\ #=> "\\"
|
|
|
|
?\u{41} #=> "A"
|
2015-09-16 00:24:31 -04:00
|
|
|
?\C-a #=> "\x01"
|
|
|
|
?\M-a #=> "\xE1"
|
|
|
|
?\M-\C-a #=> "\x81"
|
2015-09-16 01:31:26 -04:00
|
|
|
?\C-\M-a #=> "\x81", same as above
|
2015-09-16 02:16:15 -04:00
|
|
|
?あ #=> "あ"
|
2015-09-16 00:24:31 -04:00
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
See also:
|
|
|
|
|
|
|
|
* {%q: Non-Interpolable String Literals}[#label-25q-3A+Non-Interpolable+String+Literals]
|
|
|
|
* {% and %Q: Interpolable String Literals}[#label-25+and+-25Q-3A+Interpolable+String+Literals]
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
=== Here Document Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
If you are writing a large block of text you may use a "here document" or
|
|
|
|
"heredoc":
|
|
|
|
|
|
|
|
expected_result = <<HEREDOC
|
|
|
|
This would contain specially formatted text.
|
|
|
|
|
|
|
|
That might span many lines
|
|
|
|
HEREDOC
|
|
|
|
|
|
|
|
The heredoc starts on the line following <tt><<HEREDOC</tt> and ends with the
|
|
|
|
next line that starts with <tt>HEREDOC</tt>. The result includes the ending
|
|
|
|
newline.
|
|
|
|
|
|
|
|
You may use any identifier with a heredoc, but all-uppercase identifiers are
|
|
|
|
typically used.
|
|
|
|
|
|
|
|
You may indent the ending identifier if you place a "-" after <tt><<</tt>:
|
|
|
|
|
|
|
|
expected_result = <<-INDENTED_HEREDOC
|
|
|
|
This would contain specially formatted text.
|
|
|
|
|
|
|
|
That might span many lines
|
|
|
|
INDENTED_HEREDOC
|
|
|
|
|
2021-05-20 15:24:41 -04:00
|
|
|
Note that while the closing identifier may be indented, the content is
|
2012-12-16 21:26:15 -05:00
|
|
|
always treated as if it is flush left. If you indent the content those spaces
|
|
|
|
will appear in the output.
|
|
|
|
|
2015-12-07 09:39:52 -05:00
|
|
|
To have indented content as well as an indented closing identifier, you can use
|
|
|
|
a "squiggly" heredoc, which uses a "~" instead of a "-" after <tt><<</tt>:
|
|
|
|
|
|
|
|
expected_result = <<~SQUIGGLY_HEREDOC
|
|
|
|
This would contain specially formatted text.
|
|
|
|
|
|
|
|
That might span many lines
|
|
|
|
SQUIGGLY_HEREDOC
|
|
|
|
|
|
|
|
The indentation of the least-indented line will be removed from each line of
|
|
|
|
the content. Note that empty lines and lines consisting solely of literal tabs
|
|
|
|
and spaces will be ignored for the purposes of determining indentation, but
|
|
|
|
escaped tabs and spaces are considered non-indentation characters.
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
A heredoc allows interpolation and escaped characters. You may disable
|
|
|
|
interpolation and escaping by surrounding the opening identifier with single
|
|
|
|
quotes:
|
|
|
|
|
|
|
|
expected_result = <<-'EXPECTED'
|
|
|
|
One plus one is #{1 + 1}
|
|
|
|
EXPECTED
|
|
|
|
|
|
|
|
p expected_result # prints: "One plus one is \#{1 + 1}\n"
|
|
|
|
|
2012-12-18 02:46:33 -05:00
|
|
|
The identifier may also be surrounded with double quotes (which is the same as
|
|
|
|
no quotes) or with backticks. When surrounded by backticks the HEREDOC
|
|
|
|
behaves like Kernel#`:
|
|
|
|
|
|
|
|
puts <<-`HEREDOC`
|
|
|
|
cat #{__FILE__}
|
|
|
|
HEREDOC
|
|
|
|
|
2019-05-03 10:12:22 -04:00
|
|
|
When surrounding with quotes, any character but that quote and newline
|
2019-04-29 00:45:32 -04:00
|
|
|
(CR and/or LF) can be used as the identifier.
|
2019-04-28 11:24:26 -04:00
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
To call a method on a heredoc place it after the opening identifier:
|
|
|
|
|
|
|
|
expected_result = <<-EXPECTED.chomp
|
|
|
|
One plus one is #{1 + 1}
|
|
|
|
EXPECTED
|
|
|
|
|
|
|
|
You may open multiple heredocs on the same line, but this can be difficult to
|
|
|
|
read:
|
|
|
|
|
|
|
|
puts(<<-ONE, <<-TWO)
|
|
|
|
content for heredoc one
|
|
|
|
ONE
|
|
|
|
content for heredoc two
|
|
|
|
TWO
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== \Symbol Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
A Symbol represents a name inside the ruby interpreter. See Symbol for more
|
|
|
|
details on what symbols are and when ruby creates them internally.
|
|
|
|
|
|
|
|
You may reference a symbol using a colon: <tt>:my_symbol</tt>.
|
|
|
|
|
|
|
|
You may also create symbols by interpolation:
|
|
|
|
|
|
|
|
:"my_symbol1"
|
|
|
|
:"my_symbol#{1 + 1}"
|
|
|
|
|
|
|
|
Like strings, a single-quote may be used to disable interpolation:
|
|
|
|
|
2013-08-10 16:56:13 -04:00
|
|
|
:'my_symbol#{1 + 1}' #=> :"my_symbol\#{1 + 1}"
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2015-12-17 22:39:49 -05:00
|
|
|
When creating a Hash, there is a special syntax for referencing a Symbol as
|
2012-12-16 21:26:15 -05:00
|
|
|
well.
|
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
See also:
|
|
|
|
|
|
|
|
* {%s: Symbol Literals}[#label-25s-3A+Symbol+Literals]
|
|
|
|
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== \Array Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
An array is created using the objects between <tt>[</tt> and <tt>]</tt>:
|
|
|
|
|
|
|
|
[1, 2, 3]
|
|
|
|
|
|
|
|
You may place expressions inside the array:
|
|
|
|
|
|
|
|
[1, 1 + 1, 1 + 2]
|
|
|
|
[1, [1 + 1, [1 + 2]]]
|
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
See also:
|
|
|
|
|
|
|
|
* {%w and %W: String-Array Literals}[#label-25w+and+-25W-3A+String-Array+Literals]
|
|
|
|
* {%i and %I: Symbol-Array Literals}[#label-25i+and+-25I-3A+Symbol-Array+Literals]
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
See Array for the methods you may use with an array.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== \Hash Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
A hash is created using key-value pairs between <tt>{</tt> and <tt>}</tt>:
|
|
|
|
|
|
|
|
{ "a" => 1, "b" => 2 }
|
|
|
|
|
|
|
|
Both the key and value may be any object.
|
|
|
|
|
|
|
|
You can create a hash using symbol keys with the following syntax:
|
|
|
|
|
|
|
|
{ a: 1, b: 2 }
|
|
|
|
|
|
|
|
This same syntax is used for keyword arguments for a method.
|
|
|
|
|
2014-12-29 11:54:32 -05:00
|
|
|
Like Symbol literals, you can quote symbol keys.
|
2014-12-29 00:29:28 -05:00
|
|
|
|
|
|
|
{ "a 1": 1, "b #{1 + 1}": 2 }
|
|
|
|
|
|
|
|
is equal to
|
|
|
|
|
|
|
|
{ :"a 1" => 1, :"b 2" => 2 }
|
|
|
|
|
2021-12-19 11:09:52 -05:00
|
|
|
Hash values can be omitted, meaning that value will be fetched from the context
|
|
|
|
by the name of the key:
|
|
|
|
|
|
|
|
x = 100
|
|
|
|
y = 200
|
|
|
|
h = { x:, y: }
|
|
|
|
#=> {:x=>100, :y=>200}
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
See Hash for the methods you may use with a hash.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== \Range Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
A range represents an interval of values. The range may include or exclude
|
|
|
|
its ending value.
|
|
|
|
|
|
|
|
(1..2) # includes its ending value
|
|
|
|
(1...2) # excludes its ending value
|
2018-12-12 14:49:22 -05:00
|
|
|
(1..) # endless range, representing infinite sequence from 1 to Infinity
|
2019-04-03 04:11:41 -04:00
|
|
|
(..1) # beginless range, representing infinite sequence from -Infinity to 1
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
You may create a range of any object. See the Range documentation for details
|
|
|
|
on the methods you need to implement.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== \Regexp Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
A regular expression may be created using leading and trailing
|
|
|
|
slash (<tt>'/'</tt>) characters:
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
re = /foo/ # => /foo/
|
|
|
|
re.class # => Regexp
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
The trailing slash may be followed by one or more _flag_ characters
|
|
|
|
that modify the behavior.
|
|
|
|
See {Regexp options}[rdoc-ref:Regexp@Options] for details.
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
Interpolation may be used inside regular expressions along with escaped
|
|
|
|
characters. Note that a regular expression may require additional escaped
|
|
|
|
characters than a string.
|
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
See also:
|
|
|
|
|
|
|
|
* {%r: Regexp Literals}[#label-25r-3A+Regexp+Literals]
|
|
|
|
|
2012-12-16 21:26:15 -05:00
|
|
|
See Regexp for a description of the syntax of regular expressions.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== Lambda Proc Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2020-11-30 15:48:31 -05:00
|
|
|
A lambda proc can be created with <tt>-></tt>:
|
2012-12-16 21:26:15 -05:00
|
|
|
|
|
|
|
-> { 1 + 1 }
|
|
|
|
|
|
|
|
Calling the above proc will give a result of <tt>2</tt>.
|
|
|
|
|
|
|
|
You can require arguments for the proc as follows:
|
|
|
|
|
|
|
|
->(v) { 1 + v }
|
|
|
|
|
|
|
|
This proc will add one to its argument.
|
|
|
|
|
2021-12-03 08:12:28 -05:00
|
|
|
== Percent Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
Each of the literals in described in this section
|
|
|
|
may use these paired delimiters:
|
|
|
|
|
2022-05-11 16:26:10 -04:00
|
|
|
* <tt>[</tt> and <tt>]</tt>.
|
|
|
|
* <tt>(</tt> and <tt>)</tt>.
|
|
|
|
* <tt>{</tt> and <tt>}</tt>.
|
|
|
|
* <tt><</tt> and <tt>></tt>.
|
2021-12-06 17:26:28 -05:00
|
|
|
* Any other character, as both beginning and ending delimiters.
|
|
|
|
|
|
|
|
These are demonstrated in the next section.
|
|
|
|
|
|
|
|
=== <tt>%q</tt>: Non-Interpolable String Literals
|
|
|
|
|
|
|
|
You can write a non-interpolable string with <tt>%q</tt>.
|
|
|
|
The created string is the same as if you created it with single quotes:
|
|
|
|
|
|
|
|
%[foo bar baz] # => "foo bar baz" # Using [].
|
|
|
|
%(foo bar baz) # => "foo bar baz" # Using ().
|
|
|
|
%{foo bar baz} # => "foo bar baz" # Using {}.
|
|
|
|
%<foo bar baz> # => "foo bar baz" # Using <>.
|
|
|
|
%|foo bar baz| # => "foo bar baz" # Using two |.
|
|
|
|
%:foo bar baz: # => "foo bar baz" # Using two :.
|
|
|
|
%q(1 + 1 is #{1 + 1}) # => "1 + 1 is \#{1 + 1}" # No interpolation.
|
|
|
|
|
|
|
|
=== <tt>% and %Q</tt>: Interpolable String Literals
|
|
|
|
|
|
|
|
You can write an interpolable string with <tt>%Q</tt>
|
|
|
|
or with its alias <tt>%</tt>:
|
|
|
|
|
|
|
|
%[foo bar baz] # => "foo bar baz"
|
|
|
|
%(1 + 1 is #{1 + 1}) # => "1 + 1 is 2" # Interpolation.
|
|
|
|
|
|
|
|
=== <tt>%w and %W</tt>: String-Array Literals
|
|
|
|
|
|
|
|
You can write an array of strings with <tt>%w</tt> (non-interpolable)
|
|
|
|
or <tt>%W</tt> (interpolable):
|
|
|
|
|
|
|
|
%w[foo bar baz] # => ["foo", "bar", "baz"]
|
|
|
|
%w[1 % *] # => ["1", "%", "*"]
|
|
|
|
# Use backslash to embed spaces in the strings.
|
|
|
|
%w[foo\ bar baz\ bat] # => ["foo bar", "baz bat"]
|
|
|
|
%w(#{1 + 1}) # => ["\#{1", "+", "1}"]
|
|
|
|
%W(#{1 + 1}) # => ["2"]
|
|
|
|
|
|
|
|
=== <tt>%i and %I</tt>: Symbol-Array Literals
|
|
|
|
|
|
|
|
You can write an array of symbols with <tt>%i</tt> (non-interpolable)
|
|
|
|
or <tt>%I</tt> (interpolable):
|
|
|
|
|
|
|
|
%i[foo bar baz] # => [:foo, :bar, :baz]
|
|
|
|
%i[1 % *] # => [:"1", :%, :*]
|
|
|
|
# Use backslash to embed spaces in the symbols.
|
|
|
|
%i[foo\ bar baz\ bat] # => [:"foo bar", :"baz bat"]
|
|
|
|
%i(#{1 + 1}) # => [:"\#{1", :+, :"1}"]
|
|
|
|
%I(#{1 + 1}) # => [:"2"]
|
|
|
|
|
|
|
|
=== <tt>%s</tt>: Symbol Literals
|
|
|
|
|
|
|
|
You can write a symbol with <tt>%s</tt>:
|
|
|
|
|
|
|
|
%s[foo] # => :foo
|
|
|
|
%s[foo bar] # => :"foo bar"
|
|
|
|
|
|
|
|
=== <tt>%r</tt>: Regexp Literals
|
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
You can write a regular expression with <tt>%r</tt>;
|
|
|
|
the character used as the leading and trailing delimiter
|
|
|
|
may be (almost) any character:
|
|
|
|
|
|
|
|
%r/foo/ # => /foo/
|
|
|
|
%r:name/value pair: # => /name\/value pair/
|
|
|
|
|
|
|
|
A few "symmetrical" character pairs may be used as delimiters:
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
%r[foo] # => /foo/
|
|
|
|
%r{foo} # => /foo/
|
|
|
|
%r(foo) # => /foo/
|
|
|
|
%r<foo> # => /foo/
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2022-04-14 15:25:06 -04:00
|
|
|
The trailing delimiter may be followed by one or more _flag_ characters
|
|
|
|
that modify the behavior.
|
|
|
|
See {Regexp options}[rdoc-ref:Regexp@Options] for details.
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
=== <tt>%x</tt>: Backtick Literals
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
You can write and execute a shell command with <tt>%x</tt>:
|
2012-12-16 21:26:15 -05:00
|
|
|
|
2021-12-06 17:26:28 -05:00
|
|
|
%x(echo 1) # => "1\n"
|