Because I am used to Java, the general syntax will be compared with Java
Variable name: must be a single entity (do not contain spaces), must begin with a letter and underline, case sensitive. Not much different from Java
Numbers and expressions are not much different from Java
1. There will also be X + = 1, but there is no x ++ writing.
2. Pay attention to the floating point type: 10 is an integer and 10.0 is a floating point type. to_f and to_ I can be used to convert each other... integer-> struct to_s
3. Constant: Pi = 3.141952 starting with an upper-case letter, similar to the class name, should be fixed once defined
4. text string: string type. "" is used for a single line, and % Q {}is used for multiple lines. "{}" can be used <> ,() or two delimiters of your choice !!!!!! Wow !!! It can also be X = <end_my_string_please until you write
End_my_string_please: the input string ends. Of course, end_my_string_please can be customized.
4-1. String expression, with a + sign, * sign (copy n times), <,>, = (compare acⅱ code ,? A is converted into an acⅱ code, and 65. CHR is converted into a character. If I convert a string to an array, I can subtract it ~ Wow Haha ~
4-2. Insert and write a code such as # {...} into the string.
4-3. General Method of string: Capitalize, downcase, upcase, chop. Next. Hash. Reverse
4-4. regular Expression ................................... dedicated for a chapter
5. Array
Define a basic data x = [1, 2, 3, 4, 5]. In addition, the pre-defined space X = [] is not required for the array, and the type is not limited.
Insert the end of the array: <operator, or use the method to push ()
Array pop-up elements: Use the pop Method
Array-> string: If the array contains character elements, you can use join to chorus a large string x = ["A", "B", "C"] X. join-> "ABC", you can use a parameter to partition each element x. join (',')-> "A, B, C"
String-> array: "Huang Zi Hao". Scan (/\ W/) can obtain an array with one character as the element
Split can be used without face scanning. In addition, almost all built-in classes have the inspect method to render the object as "Huang Zi Hao". Split (/s ++/). Inspect
Array iteration: You can use each, collect, or traditional while... end EP: [1, 2, 4]. each {| element | puts ("the number is:" + element. to_s )}
All loops can be found. | element | obtains the iterator.
Array Operation: +,-,. Empty? ,. Include? (Xxx),. First,. Last (. First,. Last can be specified),. Reverse reverse order (remember inspect)
6. Hash
It's a key-value pair. It can be any object ~
Dictionary = {'cat' => 'feline animal ', 'dog' => 'canine animal '}
Puts dictionary ['cat']
Loop: dictionary. Each {| key, value | .......}
Operation: retrieve X. Keys. Inspect, delete X. Delete ("key"), and delete X. delete_if {| key, value |...} with conditions ....}
7. Time object
Control Structure
1. write a strange comparison syntax puts "you are too young" If age <18. The comparison operation is only <=> special (x <=> Y, x 1, y-1, equal to 0), you can also use the connector "&" "|"
The opposite of IF is unless. In addition, age.? () Functions of related classes will be learned in the future.
Let's write it normally.
If age <18
... // Insert multiple rows ~~~~
[Elseif]
[Else]
Exit
End
2. What is the three-object operator XX? XX: xx
3. Case... when... else
4. While... end
5. util... end
6. code block do... end or {}
You can write a code block by yourself.
7. the loop syntax is also quite different. 5. times do puts ("test") End, but do... end is a little friendly, but you can also use {} to replace do .. end, but the {} is used for a single row, and do is used for multiple rows .. end
In addition to this, there is also 1. upto (5) {| Number | ......................} // number is used to obtain the value of the iteration calculator (the name can be used as needed)
10. downto (5) Do | Number | ...... end
0. STEP (50, 5) {| Number | ....................} // 5 steps from 0 to 50
Loop is not for refreshing ......
8. Range
('A' .. 'Z')/(1 .. 3) can be converted to an Array Using to_a.
A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Puts a [1 .. 3]. Inspect
9. symbol http://zarknight.iteye.com/blog/126414.
A symbol does not contain values or objects. Unlike variables, it is used as a fixed name in code. The symbol does not create a new object, but only references the value.
More useful for use in a hash:
Person1 = {: Name => "Fred",: age => 20,: Gender =>: Male}
Person2 = {: Name => "fred1",: age => 21,: Gender =>: Male}
1: it is an instance of the symbol class.
2: Is the name object
3: symbols are unchangeable strings and cannot be GC
4: In most cases, the method that accepts symbols as parameters can also accept strings, which in turn do not apply;
5: saves memory by symbol than by string
6: each symbol appears only once in the object space: Like fixnum, it is saved as a direct value.
7: Interesting symbol # to_proc
RI symbol
1: it is an instance of the symbol class.
2: Is the name object
3: symbols are unchangeable strings and cannot be GC
4: In most cases, the method that accepts symbols as parameters can also accept strings, which in turn do not apply;
5: saves memory by symbol than by string
6: each symbol appears only once in the object space: Like fixnum, it is saved as a direct value.
7: Interesting symbol # to_proc
RI symbol