When would a Ruby flip-flop be useful?

烂漫一生 提交于 2019-12-17 16:16:57

问题


I think I understand how a flip-flop works thanks to a tutorial, but the example there is contrived just for teaching. Can anyone give an example of how you have actually used or would use a flip-flop?

I'm looking for a real-world application, not just another demonstration. What problems can this tool solve?

The link used to be http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators, but seems to be spam this days.


回答1:


Here's an example (taken from a rubycentral.com article) where you print out only certain lines from a file:

file = File.open("ordinal")
while file.gets
    print if ($_ =~ /third/) .. ($_ =~ /fifth/)
end

This assumes that you have a file with the following contents:

first
second
third
fourth
fifth
sixth

The program will only print out:

third
fourth
fifth

The idea is that it the value is true until the left-hand event happens, and then stays true until the right-hand event happens. If used properly this can be a nice piece of syntactic sugar, but you need to be careful to make things readable.




回答2:


I'd like to add to James' answer with some concrete examples. I've used this operator for pulling out sections of text based on regular expressions.

I was writing a tool that involved running commands on a remote server through Net::SSH. This particular server had the annoying habit of printing a MOTD regardless of whether the session was a login session or not. This resulted in getting a lot of garbage back when I ran a command and retrieved the output. As I didn't have a lot of sway in the server setup, I created a small script that printed out a delimiter, ran the program, and then printed another delimiter. The output looked something like this.

Welcome to Server X!

+----------------------------------------------------------------------+
| Use of this server is restricted to authorized users only. User      |
| activity may be monitored and disclosed to law enforcement personnel |
| if any possible criminal activity is detected.                       |
+----------------------------------------------------------------------+

----------------------------------------------
    Setting up environment for user Adam. 
----------------------------------------------

>>>>>>>>>>>>>>>>>>>>>>>>>
Program Output
<<<<<<<<<<<<<<<<<<<<<<<<<

The flip-flop operator was a useful shortcut to pull out just the section of code with the output I needed. I used a regex that matched the 25 greater-thans ">" to start the match, and 25 less-thans "<" to end the match.

output.each { |line| puts line if line[/^>{25}/] .. line[/^<{25}/] }

Output

>>>>>>>>>>>>>>>>>>>>>>>>>
Program Output
<<<<<<<<<<<<<<<<<<<<<<<<<

Most examples I've seen have involved pulling chunks of data out of a file or arrays based on regular expressions. Some other examples that come to mind are pulling out git merge conflicts, certain records from legacy flat file systems (like structs written to file), and log files.

Basically, any time you'd need to pull out sections based on beginning and ending data rather than just an individual line's content. It's a bit more complex than just a simple regex, but less complex than writing a parser.




回答3:


Odd/even line highlighting in HTML tables with many rows would seem to be a valid use-case.

I've written something not as elegant as the above several times in the past when rendering tables in Rails.



来源:https://stackoverflow.com/questions/1111286/when-would-a-ruby-flip-flop-be-useful

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!