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, like .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 connecting literals with an underscore 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 \' and \\).
String literals with white space on either side are treated as a single string literal.
p "foo" "bar" # => "foobar"
tab (0x09)
newline (0x0a)
carriage return (0x0d)
form feed (0x0c)
whitespace (0x20)
character at octal value nnn (n = 0-7)
character at hexadecimal value nn (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, which 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 denotes a regular expression option:
Regular expression matching is case insensitive.
Multiple run 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 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.
Converted from CHM to HTML with chm2web Pro 2.85 (unicode) |