1. If print does not automatically wrap the line, puts will automatically wrap the line. If one line is not enough to write, you can add "\" to connect.
Ruby code
Puts 6/2 print 6/1 puts 'Hello world' puts. We are all '\ 'China'
Running result:
Ruby code
36 hello world we are all Chinese
2. =, eql ?, Equal? Differences
= Equal Value eql? Equal value, equal type equal? Equal value, equal memory address
A = 1b = 1.0c = 1.0d = 1.0e = cputs (a = B) # equal puts (a. eql? (B) # equal value, equal type puts (c. equal? (D) # equal value, equal memory address puts (c. equal? (E ))
Running result:
Ruby code
truefalsefalsetrue
3. # <=> compare the sizes of two objects, greater than, equal to, and less than 1, 0,-1, respectively.
#=== Whether the right object is in the left area, true or false is returned
Ruby code
puts("abd" <=> "acd")puts((0..5) === 10)puts((0..5) === 3.2)
Running result:
-1falsetrue
4. case branch Condition Statement
X = 3 case xwhen 1 .. 2 print "x =", x, ", in 1 .. 2 "when 4 .. 9, 0 print "x =", x, ", in 4 .. 9, 0 "elseprint" x = ", x,", other possibilities "end output result: x = 3, other possibilities
5. while and
Note:
A: The string connection in ruby uses A comma (the plus sign is used in java), for example:
Puts ("aaa", "bbb ")
Print ("aaa", "bbb ")
Output aaa line breaks bbb and aaabbb respectively.
B: There seems to be no ++ in ruby, so a ++ is wrong. Only a = a + 1 can be used.
Ruby code
A = 1 while (<10) Print (a, "")= A + 1Endb= 1Until (B>= 10) print (B, "") B = B + 1end output result: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
6. Several Functions
Print "aa" if I <2
Equivalent
If I <2
Print "aa"
End
Ruby code
3. times {print "hi"} 1. upto (9) {| I | print I if I<7># | I | can be considered as a temporary reference variable for later use 9. downto (1) {| I | print I if I<7>(1 .. 9). each {| I | print I if I<7>0. step (11,3) {| I | print I} running result: hihihi123456654322133456036