Transferred from [foreverphoenix column]
Number
1. Ruby supports integers and floating-point numbers. An integer can contain any length. An integer within a certain range is an object of the fixnum class. Integers out of the range are stored in bignum class objects. This processing is transparent.
2. You can add a question mark before an ASCII character to obtain the corresponding integer. Can CTRL be used? /C-X or? /Cx (X & 0x9f. Meta characters (X | 0x80) can be? /M-X.
3. a numeric literal with a decimal point "or/or" index is considered a float object. The float object is the same size as the double type on the machine.
4. Float precision is limited. If you need a better precision decimal, you can use bigdecimal in the standard library, but the efficiency will be affected.
String
1. Various escape sequences can be placed in the string literal. The type of the string delimiter determines the degree of replacement. In a single quotation mark string, only the backslash and single quotation mark escape are identified. Double quotation marks support more escape characters. You can also use # {expr} to put the value of any Ruby expression into the string. If expr is a global variable, a class variable, or an instance variable, curly braces can be ignored.
2. There are three other forms of string literal construction: % Q (equivalent to single quotation marks), % Q (equivalent to double quotation marks), here document. The character following Q or Q is a separator. The delimiter can be a single-byte character other than letters or numbers. The string is read until the next same (for {[<, It is the corresponding closed symbol) delimiter appears. Here document consists of the rows in the source file that are not included in the <character indicating the end string. Generally, the Terminator must appear in the first column. If you place a minus sign after the <character, you can indent the Terminator. The delimiter can span multiple lines. The line terminator and the spaces at the beginning of the subsequent line are included in the string.
3. Other separators used for string generation include % W (% W) to generate string array, % x to execute shell command, and % R to generate regular expression.
4. the ruby string is an 8-bit byte sequence. To process unicode encoding, add the following code: (The jcode library overwrites most methods in the string class to process multi-byte text) $ kcode = 'U' require 'jcode'
5. Regular Expressions: 1)/regular_expression/2) Regexp. New ("regular_expression") 3) % R {regular_expression}
Interval
1. As a sequence, "..." is used to create closed intervals (including values on the right ),"..." The form is to create a semi-closed semi-open interval (excluding the right-end value ).
2. You can use custom objects to create intervals. The condition is that these objects must implement the succ method and <=>.
3. The interval is used as a condition. If the condition in the first part of the interval is true, it is enabled. If the condition in the second part of the interval is true, it is disabled. When the range of two points changes from off to off for the first time, the ending condition is solved immediately and the state changes accordingly. This means that if both expr1 and expr2 are true in one call, the two-point call will end in the off state. However, the call still returns true. The three-point range does not solve the ending condition immediately after it enters the open state.
4. Use the interval as the interval to test whether some values fall into the interval of expression. Use the = Operator to compare.
Array
1. the array class literal is composed of a series of object references separated by commas between square brackets. For arrays of all string objects, % W and % W can be abbreviated. In % W format, the token separated by spaces is extracted as a continuous array element without replacement within a single string. The % W format performs the same replacement rule for each token as the double quotation mark string. spaces can be escaped using backslashes.
2. Access array elements through [Index]. The index of the first element is 0. The index can be a negative value.-1 indicates the last element.
Hash
1. Hash list initialization can be performed in the form of key => value in {}. Keys and values are continuously placed in [], separated by commas.
2. If the keys in the hash table use equ? The two keys must have the same hash code. If the user-defined class object instance is used as the hash key, make sure that 1) Once the object is created, its hash code will not change; 2) call the hash # rehash method to re-index the hash list whenever the hash code of the key changes.
Symbol
1. In Ruby, symbol indicates a "name", such as a string name and an identifier name. To create a symbol object, add a colon Before the name or string. Each string object is different, even if they contain the same string content, a name (string content) uniquely identifies a symbol object. If the name is the same, the symbol is the same. If the name is different, the symbol is different. Once defined, the symbol object will exist until the program exits. All symbol objects are stored in the symbol table in Ruby. You can use the class method symbol. all_symbols to obtain all symbol objects defined in the current Ruby program. This method returns an array of symbol objects.