Read <<programming ruby>> 7.6 Verses flip-flop understanding

Source: Internet
Author: User

This is the source code in the book.

File.foreach ('1.txt') do |x|  if (($. = = 1) | | x =~/eig/). (($. = = 3) | | x =~/nin/)     Then Print x  endend

Where 1.txt content is as follows

Firstsecondthirdfourthfifthsixthseventheigthninthtenth

Read the first,$ of the first line. should be 1 ($. is a global variable that represents the line number) but RubyMine is not found in 1, this I did not find the reason for the moment. So I was prompted by others. I changed it.

f = file.new (' 1.txt ') F.each do |line|  if ((F.lineno = = 1) | | Line.chomp =~/eig/): ((f.lineno== 3) | | line.chomp =~/nin/) then    print F.lineno    print line  endend

  

This f.lineno indicates that each read line number runs the test result as follows

Only 2 points were mentioned in the book.

1 Exp1. Expr2 such a rang, before EXP1 becomes true, its value is false, before EXP2 becomes true, rang is solved as true
2 This fragment of code. is to print between 1 and 3 lines and between/eig/and/nin/

A little obscure, and then a crowd of people prompted to read this article: http://nithinbekal.com/posts/ruby-flip-flop/

The flip flop operator is a range ( .. ) operator used between, conditions in a conditional statement. It looks like this:

(1..20). Each do |x|  Puts x if (x = = 5): (x = = Ten) end

 The condition evaluates to false every time it was evaluated until the first part evaluates to True. Then it evaluates-true until the second part evaluates to True. In the above example, the flip-flop was turned on while x == 5 and stays on until x == 10 , so the numbers from 5 to Printed.

(x = = 5).. (x = = 10) This thing is called the flip flop.

1 Blue English means when the first part is (x==5) true. The whole if judgment is true, but when is false. Until the second part is true (x==10), that  is the EXP1 mentioned earlier. Expr2 such a rang, before EXP1 becomes true, its value is false, before EXP2 becomes true, rang is solved as true 2  red words in English means x==5, this expression begins to execute. When to stop and know x==10, so print between 5 and 10

  

So understand

((F.lineno = = 1) | | line.chomp =~/eig/) ... ((f.lineno== 3) | | Line.chomp =~/nin/)

You can think of this, when the F.lineno value is 1, the expression ((F.lineno = = 1) | | Line.chomp =~/eig/) is true, the whole if is true,

Then print 1first, when does the print end? , when the value of Flineno=3 or Line.chomp (Chomp minus the end of the line/r/n) matches the/nin/, the two of them will not print when they meet one of them.

Can be understood like the code below

f = file.new (' 1.txt ') F.each do |line|  if ((1..3) Include?f.lineno) or (line =~/eig/or line =~/nin/) then    print F.lineno    print line  endend

  

Read <<programming ruby>> 7.6 Verses flip-flop understanding

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.