Values that can be expressed directly in Ruby programs, such as the number 1 or the string "hello world", are called literals.
integer
integer (signed)
floating-point number
Floating-point numbers beginning with a decimal point, such as .1, are not allowed. They must be written with a leading zero (0.1).
floating-point number
hexadecimal integer
binary integer
octal integer
Numeric literals can contain an underscore (_). The Ruby interpreter simply ignores these underscores and does not interpret them in a special way. This can be useful as a thousands separator for large values. However, placing an underscore before and after a literal or using consecutive underscores will result in an error.
1_000_000_000 # => 1000000000 0xffff_ffff # => 0xffffffff
Example:
"this is a string expression\n" 'this is a string expression'
String expressions begin and end with double or single quote marks.
Double-quoted string expressions are subject to backslash notation and expression substitution. Single-quoted strings are not (except for \'(single quotation) and \\(backslash itself)).
String literals with a space on either side are treated as a single string literal.
p "foo" "bar" # => "foobar"
The backslash character (\) is the same as the yen sign (¥) used in Japanese fonts.
tab (0x09)
newline (0x0a)
carriage return (0x0d)
form feed (0x0c)
backspace (0x08)
bell (0x07)
escape (0x1b)
space (0x20)
character at octal value nnn (n = 0-7)
character at hexadecimal value xnn (n = 0-9, a-f)
In double-quoted strings and regular expressions, the form "#{expression}" can be extended to the (string of the) contents of that expression. If the expressions are variables beginning with either $ or @, the surrounding braces may be omitted and the variable can be expressed as a #variable. The character # is interpreted literally if it is not followed by the characters {, $, or @. To explicitly prevent expression substitution, place a backslash in front of the #.
$ruby = "RUBY" p "my name is #{$ruby}" # => "my name is RUBY" p 'my name is #{$ruby}' # => "my name is #{$ruby}"
Example:
/^Ruby the OOPL/ /Ruby/i /my name is #{myname}/
Strings delimited by slashes are regular expressions. Regular expressions are instances of the Regexp class.
Refer to Regular Expressions for more information on which metacharacters are interpreted as regular expressions.
The characters immediately following the final slash denote a regular expression option as follows:
The matching regular expression is not case sensitive.
Multiple line mode. Newlines are treated as normal characters (matching with the . character).
Ruby correctly handles multibyte characters (such as Chinese and Japanese) in regular expressions.
Backslash notation and expression substitution are available in regular expressions, as in strings.
If a regular expression literal does not include expression substitution, it will return the same regular expression object every time it is evaluated. If expression substitution is included, the regular expression will be compiled with every evaluation (based on the expression substitution results) and a new regular expression object will be created.
Example:
[1, 2, 3]
Syntax:
'[' expr ',' ... ']'
Returns an array containing the result of each expression. Arrays are instances of the class Array.
Array expressions spawn a new array object every time they are evaluated.
Example:
{1=>2, 2=>4, 3=>6}
Syntax:
'{' expr '=>' expr ',' ... '}' '{' expr ',' expr ',' ... '}'
Returns a new hash object that maps each resulting value to a key. Hashes are instances of the class Hash.
A hash (also called an associative array) can associate one object of an arbitrary type with another.
Hash expressions spawn a new hash object every time they are evaluated.
Example:
1 .. 20
Syntax:
expr1 '..' expr2 expr1 '...' expr2
If a range expression appears anywhere but a conditional expression, returns the range object from expression 1 to expression 2. Range objects are instances of the class Range.
Range objects spawned by .. operators include the final expression, while those spawned by ... operators do not.
If both ends of a range expression are numeric literals, the expression will return the same object every time it is evaluated. Otherwise, the expression will return a new range object every time it is evaluated.
Example:
:class :lvar :method :$gvar :@ivar :+
Syntax:
':' identifier ':' variable name ':' operator
Returns symbols that have a one-to-one correspondence with arbitrary strings. Symbols are an instance of the Symbol class.
Symbols are unique objects that return the same object every time they are evaluated.