Literals

From RubySpec

Jump to: navigation, search

Contents

Numbers

Ruby supports three types of literal numbers with multiple means of specification.

Fixnums

Fixnums, encapsulated in the Fixnum type, are signed integer numbers which will fit within the native machine word size. Often several bits in this word are reserved for flags, and thus cannot be used for the Fixnum. How many flag bits there are is implementation dependent, but the main Ruby interpreter uses 1 flag bit in the case of Fixnums. So for that interpreter, on a 32-bit machine, a Fixnum is a 31-bit signed integer.

Fixnums can be specified in the usual ways and in a number of Ruby-specific ways.

Standard notation
0
1234
-576
+3
Embedded underscores
123_456_789
-5_5_5_2_3_1_2
  • Fixnums may not be preceded or followed by an underscore
  • Limit one underscore per delimiter
  • Underscores may not precede or follow base notation characters 'x' or 'b' below
Base notations
0xabcd # Hex
0xab_cd # Hex with embedded underscore
0xABCD # Caps are ok
-0xabcd # Negative hex
0177 # Octal
0_1_77 # Octal with embedded underscores
-0177 # Negative octal
0b10101 # Binary
-0b10101 # Negative binary
Character and control codes
?a # character code for 'a' character
?\C-a # code for Control-a
?\M-a # code for Meta-a

Floats

Floats represent 64-bit IEEE floating point numbers and are encapsulated by the Float type. They are specified like standard Fixnums containing an embedded decimal point and/or an embedded exponentiation delimiter 'e'.

123.456
3.141_592_653_589_793_238_462
-4.5
6.5e3
-.4_5_6e10
  • As with Fixnums, underscores can only follow or precede digits, and may not start or end the integer part, the decimal part, or the exponent of a literal float

Bignums

Bignums are arbitrary precision signed integers, scalable to virtually any size and encapsulated in the Bignum type. Any literal integer larger than the representable values of Fixnum is represented as a Bignum. Bignums are subject to the same formatting rules as Fixnums.

Standard notation
112233445566778899112233445566778899
-999999999999999999999999999999999999
Embedded underscores
111_222_333_444_555_666_777_888_999_000
1_1_2_3_5_8_13_21_34_55_89_144_233_377
Base notations
0xaaaaaaaaaaaaaaaaaaaaaaaaa
077777777777777777777777777777777777777777
0b1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101

Symbols

Symbols are literally represented in code as a colon preceding a string of characters, usually alphanumeric (actually usually alpha) but also including a number of accepted non-alphanumeric characters usable in symbols. They are encapsulated by the Symbol type. At runtime, however, symbols are characterized by their atomicity and uniqueness; the same symbol in multiple places in code is guaranteed to be the same object with the same properties. For this reason they are used to describe method names, variable names, and other language elements that must be unique and atomic across all references.

:this_is_a_symbol
:"and so is this"
:+ # some non-alphanumeric characters are allowed, 
   # primarily for the purpose of defining overloaded operators

Symbols are atomic; that is, their values cannot be modified at runtime.

Strings

Strings are encapsulated in the String class and can be represented in several literal forms. Several of the forms have different semantics when a string has embedded Ruby code.

"Double quoted string"
'Single quoted string'
"Double quoted can embed Ruby code using #{do something} notation"
"Double quoted can even embed #{"double quoted strings"} inside the embedded code notation"
%q(This is a single-quoted string)
%Q(This is a double-quoted string)
%(This is also a double-quoted string)
%!Exclamation can be replaced with any non-alphanumeric char (double-quoted string)!
% Even_whitespace_characters_can_be_used! 
%q[The string boundaries can be almost any character; that character or those characters
must be escaped within the string. Here I use square brackets instead of parenthesis.]

Where double-quoted strings can embed code using the #{...} syntax, single quoted strings can not. As illustrated above, the embedded code can itself contain double-quoted strings without escaping the inner double-quotes.

Ruby also allows the concept of "HERE" documents or strings, which are strings delimited by an arbitrary token

x = <<END_OF_STRING
All this content will
be part of the string
with newlines intact.
HERE strings are
double quoted by default.
END_OF_STRING

If a HERE-doc string is defined with a <<- instead of a <<, the matching end token is allowed to be anywhere on a line, rather than just at the beginning of a line. It does still need to be the only thing on the line. You may also specify that the HERE-doc should behave as a single-quoted string by surrounding the opening token with single quotes.

y = <<-'SINGLE_QUOTED'
  Specify single-quoted HERE strings in this way.
  SINGLE_QUOTED

Many non-printable or non-embeddable characters can be inserted into strings using a backslash notation. Here's a list of the useful backslash characters:

\t (tab), \n (newline), \r (carriage return), \f (form feed), \b (backspace),
\a (bell), \e (escape), \s (whitespace), \nnn (octal), \xnn (hexadecimal),
\cx (control x), \C-x (control x), \M-x (meta x), \M-\C-x (meta control x)

Arrays

Arrays hold object references and are encapsulated by the Array type. They are specified with a list of objects between square brackets.

[ 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
[ 82_000, "rum", 32 ]

Arrays of String can also be created using the %w construct, which splits space-delimited tokens into String-encapsulated array elements. Spaces can be escaped to prevent tokenization.

%w( dog cat fish monkey platypus man great\ owl )

Hashes

 { 1 => "bun", 2 => "shoe", 3 => "tree"}

When a hash is the last parameter in a method invocation, apart from an optional block one may leave off the braces. In this way one may use a hash as a means of providing options to methods.

 def method_with_options(p1,p2) ; puts p1.class ; puts p2.class ;  end
 method_with_options(123, :these=>'two parameters are passed', :as=>'a single hash')

outputs

 Fixnum
 Hash

Regular Expressions

Regular expressions hold object references and are encapsulated by the Regexp type. They are specified with a sequence of regular expression atoms between forward slashes, or using the %r construct.

/foo/
/^(b|a|r)/
%r![a-z_]([a-z0-9_])*!
Personal tools