Basic Process Control and basic Ruby knowledge process

Source: Internet
Author: User
Tags switch case

Basic Process Control and basic Ruby knowledge process

(1) condition judgment
Condition judgment is the basic formula:
Copy codeThe Code is as follows:
If expression
Code
End

If the expression is not false or nil, the code part can be executed. For else, if it is false or nil, the code under else is executed. Note that there must be a line break between the expression and the Execution Code, for example, line feed, semicolon, or then keyword.
In the case of multi-condition branches, besides else if, elsif can be omitted.
Copy codeThe Code is as follows:
If expression1
Code1
Elsif expression2
Code2
Else
Code3
End

If then is used as the delimiter:
If expression then code end, you can directly use if as the boundary: code if expression. In this case, if is called an expression modifier. Write execution before writing execution conditions. NOTE: if it becomes an expression modifier, no line break is allowed between the Execution Code and if. Although this method is also a conditional sentence, I think it is more like a modifier.

For if judgment, it can return the value after code execution.

Similar to the if condition, there is an unless judgment, which is opposite to the if condition.

In C #, multiple branch statements have a switch case switch. In ruby, it is case when. Case when is flexible.

(2) loop

The loop condition is while do or until do, And the loop body is located between them.
The condition if is similar, and the loop statement can also be compact in the form of delimiters. In this case, line breaks and end are omitted.
Copy codeThe Code is as follows:
X = 1
Puts x, x + = 1 while x <10
 
While x <20 do
Puts x
X + = 1
End

 
In C #, The for loop is widely used. In ruby, The for loop is similar to the foreach function and is used to iterate on the enumerated objects.
Copy codeThe Code is as follows:
For var in collection do
Code
End

Collection is an object with an each iteration method. The do keyword is optional (can be replaced by a line break or semicolon ).
Copy codeThe Code is as follows:
Arr = % w [1 2 3 4 5 6]
For item in arr
Print item <""
End
 
Hash1 = {: a => 1,: B => 2,: c => 3}
For key, value in hash1
Print "# {key }=# {value}" <""
End

(3) iterator and enumeration
In addition to loop control while, for, and until, there is a special method for loop, that is, the iterator. It is one of ruby's most important features.
(1) numerical iterator
Upto: calls the associated code block for All integers in a range. Start on the left and end on the right.
Downto: opposite to upto.
Times: code that calls a link for a certain number of times and passes 0 to n-1 to the code.
Step: increment an iteration to a specified value at a certain step. The second parameter is the step size.
Copy codeThe Code is as follows:
1. upto (5) {| x | print "# {x }"}
Puts
5. downto (1) {| x | print "# {x }"}
Puts
3. times {| x | print "# {x }"}
Puts
3. step (5, 0.5) {| x | print "# {x }"}
 
(2) Enumeration iterator
Each: Pass the iteration element to the code.
Collect: After executing the associated code for each element that calls its enumeration, the code is combined and returned as an array.
Select: After executing the associated code for each element that calls its enumeration, if it is true, it is combined to return an array.
Reject: opposite to select. It returns the element false or nil as a data.
Inject: calculates the cumulative value iteration. Two parameters are used to call the associated code block. The first parameter is the cumulative value of the previous call code block, and the second parameter is the next element of the call iteration. If a parameter is passed, it is used as the initial value of the first parameter. If not, the element value of the first iteration is used as the initial value.
Copy codeThe Code is as follows:
A1 = [1, 2, 3]
A2 = a1.map {| x + 1}
A3 = a2.collect {| x + 1}
Print a2, a3
 
# O even
A5 = a1.select {| x % 2 = 0}
Print a5
 
A6 = a1.reject {| x % 2 = 0}
Print a6
Puts
A7 = a1.inject {| sum, x | x + sum}
Puts a7
A8 = a1.inject (10) {| sum, x | x + sum}
Puts a8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.