Basic Process Control of Ruby Basics _ruby Topics

Source: Internet
Author: User
Tags modifier switch case

(i) Conditional judgment
the condition is judged as the basic type:

Copy Code code as follows:

If expression
Code
End

If the expression is not false or nil, then the Code section can be executed, so if it is false or nil for else, then that is executed. Notice that there is a delimiter between the expression and the execution code: for example, a newline or semicolon or a then keyword.
In the case of a multiple conditional branch, an ellipsis can be used except that you can use else if: elsif
Copy Code code as follows:

If expression1
Code1
Elsif expression2
Code2
Else
Code3
End

If you are using then as a delimiter:
If expression then the code end, then you can make the if directly a delimiter: code if expression, at which point the if is called an expression modifier. Write execution and write implementation conditions first. Note that if you become an expression modifier, you cannot have line breaks between the execution code and the IF. Although this approach is also a conditional sentence, but I think it is more like a decoration.

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

Similar to if conditions, there are unless judgments, which are the opposite of If.

In C #, the multiple-spoke statement has a switch case switch, and in Ruby it is the case. The case is very flexible.

(ii) Circulation

The loop condition is the while do or until do, and the loop body is between them.
If the present condition is similar, the circular statement can also be done in a compact loop in the form of a delimiter, at which point the line break and end are omitted.

Copy Code code as follows:

X=1
Puts X,x+=1 while x<10

While x<20 do
Puts X
X+=1
End


A For loop application is very extensive in C #, and in Ruby, for loops are similar to foreach's, for iterating over enumerable objects.
Copy Code code as follows:

For Var in collection do
Code
End

Where collection is an object with each iteration method, the Do keyword is optional (you can replace it with a newline character or a semicolon).
Copy Code code 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

(iii) Iterators and enumerations
in addition to loop control while, for, and until, there is a special method for looping, the iterator, which is one of the most important features of Ruby.
(1) Numerical iterator
Upto: Calls its associated code block for all integers in an interval. The left is the start and the right is the end.
Downto: the opposite of upto.
Times: Call the code of the relationship a certain number, and pass 0 to n-1 to the code.
Step: Increments the iteration to the specified number with a certain length. The second parameter is the step.

Copy Code code 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) Enumerating iterators
Each: Pass an iteration element to the code.
Collect: After executing the associated code for each element of the enumeration that invoked it, it is grouped together as an array to return.
Select: After executing the associated code for each element of the enumeration that invoked it, if true, combine together as an array to return.
Reject: The opposite of a select. It returns the element of false or nil as a data.
Inject: Calculate the cumulative value iteration. The associated code block is invoked with two parameters, the first parameter is the cumulative value of the last called code block, and the second parameter refers to the next element of the call for the iteration. If there is a pass parameter, it is the initial value of the first argument, and if not, the first element value to be iterated is the initial value.
Copy Code code as follows:

a1=[1,2,3]
A2=a1.map{|x| X+1}
A3=a2.collect{|x| X+1}
Print A2,a3

#o偶数
A5=a1.select{|x|x%2==0}
Print A5

A6=a1.reject{|x|x%2==0}
Print A6
Puts
A7=a1.inject{|sum,x|x+sum}
Puts A7
A8=a1.inject (Ten) {|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.