Now let's take the previous examples Program Of Code Let's take a look.
The following example is shown in the simple example section.
Def fact (N)
If n = 0
1
Else
N * fact (n-1)
End
End
Print fact (argv [0]. to_ I), "\ n"
Because this is the first explanation, we will analyze it row by row.
Def fact (N)
In the first line, Def is used to define a function (or, more accurately, a method); we will discuss in detail what is a method later ). here, it indicates that the fact function carries a parameter, that is, N.
If n = 0
If is used to check a condition. When the condition matches, execute the following code; otherwise, execute the Code following Else.
1
When the condition is set, the value of IF is 1.
Else
If the condition is not true, run the code from here to end.
N * fact (n-1)
If the condition is not met, the IF value is the result of N multiplication fact (n-1.
End
The first end corresponds to the IF statement.
End
The second end corresponds to the def statement.
Print fact (argv [0]. to_ I), "\ n"
Use the value specified by the command line to call the fact () function and print the result.
Argv is an array containing command line parameters. argv is a string member, so we must convert it to an integer through to_ I. Ruby does not automatically convert the string to an integer like Perl.
Hmmm... what if a negative value is assigned to the program as a parameter? Have you seen this problem? Can you fix it?
Strings
Next, let's check the riddle program that appears in the character string chapter. Because this is longer, we add the number of rows for each row.
01 words = ['foobar', 'baz', 'quux ']
02 secret = words [rand (3)]
03
04 print "Guess? "
05 while guess = stdin. Gets
06 guess. Chop!
07 if guess = secret
08 print "You win \ n"
09 break
10 else
11 print "you lose. \ n"
12 End
13 print "Guess? "
14 End
15 print "the word is", secret, ". \ n"
In this program, we use a new control structure while. As long as a specified condition remains true, the code between the while and its end will be executed repeatedly.
Rand (3) of Row 2 returns a random number between 0 and 2. This random number is used to extract a member from the array words.
In row 5, we use stdin. the gets method reads a row from the standard input. if the read row encounters an EOF (end of the file), gets will return nil. therefore, the code connected to the while statement will be executed until it encounters ^ d (or ^ Z under DOS), indicating the end of the input.
Line 6 guess. Chop! Remove the last character of guess; it must be a line break.
Row 15: Let's print the words to guess. the code we write is to pass three parameters to the print statement (the three parameters are printed one by one), but it can also be printed using one parameter: write secret as # {secret} to indicate that it is a variable to be taken, rather than a general text to be printed:
Print "the word is # {secret}. \ n"
Regular Expression
Finally, let's look at the program in the regular expression section.
01 st = "\ 033 [7 m"
02 en = "\ 033 [M"
03
04 while true
05 print "str>"
06 stdout. Flush
07 STR = gets
08 break if not Str
09 Str. Chop!
10 print "Pat>"
11 stdout. Flush
12 Re = gets
13 break if not re
14 re. Chop!
15 STR. gsub! Re, "# {st }\\ {en }"
16 print STR, "\ n"
17 end
18 print "\ n"
In Row 4, The while condition is hard set to true, so it seems to constitute an infinite loop. however, we place the break statement in rows 8 and 13 to exit the loop. these two break statements are also an example of if modifier. A "If modifier" is executed only when the specified condition is met.
Let's talk about chop! (Lines 9 and 14). In Ruby, we can also set "! "And "? "Appended to some method names. Exclamation point (!, Sometimes I read it as "Bang! ") Implies that something may be destructive (destructive), that is, something can change what it involves. Chop! Directly act on a string, but it does not contain! Only one copy is generated. The following is a demonstration of this difference.
Ruby> S1 = "forth"
"Forth"
Ruby> s1.chop! # This changes S1.
"Fort"
Ruby> S2 = s1.chop # This puts a changed copy in S2,
""
Ruby> S1 #... without disturbing S1.
"Fort"
You will see a question mark (?, Sometimes, I read "huh? ") End method name; this refers to the" prediacte "(prediacte) method, only true or false is returned.
Line 15 should be noted. First, pay attention to gsub! It is also a damage function. it modifies STR by replacing all characters that match the re pattern (sub indicates replacement, and the first letter G indicates global characters, for example, replacing all matches instead of the first match ). so far, it's okay; but what should we use to replace the matching part in the text? The St and EN in Row 1 and row 2 are defined as the ANSI codes that indicate the reversed text color (color-inverted) and restored normal text color, respectively. in row 15, they are enclosed by # {} to ensure that they are interpreted as defined above (so that we don't see the variable name printed out ). in the middle is "\\&". this is a trick. because the replacement string is caused by double quotation marks, a backslash is interpreted as a single backslash. Therefore, gsub! The actual result is "\ &". A special code exactly represents "any character that matches the pattern at the first place ". therefore, when a new string is printed, it is similar to the original one, but the matching part is displayed in an inverse video mode.