# Ruby Programming/Syntax/Literals
## Numerics

Note: the meaning of "?x" notation has been changed. In ruby 1.9 this means not an ASCII numeric code but a string i.e. ?a == "a"

## Strings

Examples:
A string expression begins and ends with a double or single-quote mark. Double-quoted string expressions are subject to backslash notation and interpolation. A single-quoted string expression isn't; except for \' and \\.

### Backslash Notation

Also called escape characters or escape sequences, they are used to insert special characters in a string.
Example:
| Escape Sequence | Meaning |
| \n | newline (0x0a) |
| \s | space (0x20) |
| \r | carriage return (0x0d) |
| \t | tab (0x09) |
| \v | vertical tab (0x0b) |
| \f | formfeed (0x0c) |
| \b | backspace (0x08) |
| \a | bell/alert (0x07) |
| \e | escape (0x1b) |
| \nnn | character with octal value nnn |
| \xnn | character with hexadecimal value nn |
| \unnnn | Unicode code point U+nnnn (Ruby 1.9 and later) |
| \u{nnnnn} | Unicode code point U+nnnnn with more than four hex digits must be enclosed in curly braces |
| \cx | control-x |
| \C-x | control-x |
| \M-x | meta-x |
| \M-\C-x | meta-control-x |
| \x | character x itself (for example \" is the double quote character) |
For characters with decimal values, you can do this:
"" << 197 # add decimal value 197 to a string
=> Å
or embed them thus:
"#{197.chr}"

### Interpolation

Interpolation allows Ruby code to appear within a string. The result of evaluating that code is inserted into the string:
The expression can be just about any Ruby code. Ruby is pretty smart about handling string delimiters that appear in the code and it generally does what you want it to do. The code will have the same side effects as it would outside the string, including any errors:

### The % Notation

There is also a Perl-inspired way to quote strings: by using % (percent character) and specifying a delimiting character, for example:
Any single non-alpha-numeric character can be used as the delimiter, %[including these], %?or these?, %~or even these things~. By using this notation, the usual string delimiters " and ' can appear in the string unescaped, but of course the new delimiter you've chosen does need to be escaped. However, if you use %(parentheses), %[square brackets], %{curly brackets} or %<pointy brackets> as delimiters then those same delimiters can appear unescaped in the string as long as they are in balanced pairs:
A modifier character can appear after the %, as in %q, %Q, %x - these determine how the string is interpolated and what type of object is produced:
| Modifier | Meaning |
| %q[ ] | Non-interpolated String (except for \\, \[, and \]) |
| %Q[ ] | Interpolated String (default) |
| %r[ ] | Interpolated Regexp (flags can appear after the closing delimiter) |
| %i[ ] | Non-interpolated Array of symbols, separated by whitespace (after Ruby 2.0) |
| %I[ ] | Interpolated Array of symbols, separated by whitespace (after Ruby 2.0) |
| %w[ ] | Non-interpolated Array of words, separated by whitespace |
| %W[ ] | Interpolated Array of words, separated by whitespace |
| %x[ ] | Interpolated shell command |
| %s[ ] | Non-interpolated symbol |
Here are some more examples:

### "Here document" notation

There is yet another way to make a string, known as a 'here document', where the delimiter itself can be any string:
The syntax begins with << and is followed immediately by the delimiter. To end the string, the delimiter appears alone on a line.
There is a slightly nicer way to write a here document which allows the ending delimiter to be indented by whitespace:
To use non-alpha-numeric characters in the delimiter, it can be quoted:
Here documents are interpolated, unless you use single quotes around the delimiter.
The rest of the line after the opening delimiter is not interpreted as part of the string, which means you can do this:
You can even "stack" multiple here documents:
And you can even apply methods:

## Command Expansion

You can execute shell commands and run any external programs, and get the output, with backticks-quoted strings.

## Arrays

An array is a collection of objects indexed by a non-negative integer. You can create an array object by writing Array.new, by writing an optional comma-separated list of values inside square brackets, or if the array will only contain string objects, a space-delimited string preceded by %w.
The last method, using %w, is in essence shorthand for the String method split when the substrings are separated by whitespace only. In the following example, the first two ways of creating an array of strings are functionally identical while the last two create very different (though both valid) arrays.

## Hashes

Hashes are basically the same as arrays, except that a hash not only contains values, but also keys pointing to those values. Each key can occur only once in a hash. A hash object is created by writing Hash.new or by writing an optional list of comma-separated key => value pairs inside curly braces.
Usually Symbols are used for Hash keys (allows for quicker access), so you will see hashes declared like this:
The latter form was introduced in Ruby 1.9.

### Hash ordering

Note that with 1.8, iterating over hashes will iterate over the key value pairs in a "random" order. Beginning with 1.9, it will iterate over them in the order they were inserted. Note however, that if you reinsert a key without first deleting it, or change an existing key's value, the key's order in iteration does not change.

## Ranges

A range represents a subset of all possible values of a type, to be more precise, all possible values between a start value and an end value.
This may be:
* All integers between 0 and 5.
* All numbers (including non-integers) between 0 and 1, excluding 1.
* All characters between 't' and 'y'.
In Ruby, these ranges are expressed by:
Therefore, ranges consist of a start value, an end value, and whether the end value is included or not (in this short syntax, using two . for including and three . for excluding).
A range represents a set of values, not a sequence. Therefore,
though syntactically correct, produces a range of length zero.
Ranges can only be formed from instances of the same class or subclasses of a common parent, which must be Comparable (implementing <=>).
Ranges are instances of the Range class, and have certain methods, for example, to determine whether a value is inside a range:
For detailed information of all Range methods, consult the Range class reference.
Here is a tutorial on their use.