简体   繁体   English

Ruby触发器什么时候有用?

[英]When would a Ruby flip-flop be useful?

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. 以前的链接是http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators ,但这些天似乎是垃圾邮件。

Here's an example (taken from a rubycentral.com article ) where you print out only certain lines from a file: 这是一个示例(取自rubycentral.com文章 ),您只打印文件中的某些行:

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. 如果使用得当,这可能是一个很好的语法糖,但你需要小心,使事情可读。

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. 我正在编写一个工具,涉及通过Net :: SSH在远程服务器上运行命令。 This particular server had the annoying habit of printing a MOTD regardless of whether the session was a login session or not. 无论会话是否是登录会话,这个特定服务器都有打印MOTD的烦人习惯。 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. 我使用了匹配25个大型“>”的正则表达式来开始比赛,并且使用了25个较少的“<”来结束比赛。

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. 想到的其他一些例子是从旧的平面文件系统(如编译到文件的结构)和日志文件中提取git合并冲突,某些记录。

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. 它比一个简单的正则表达式复杂一点,但比编写解析器复杂一点。

Odd/even line highlighting in HTML tables with many rows would seem to be a valid use-case. 具有许多行的HTML表中的奇数/偶数行突出显示似乎是一个有效的用例。

I've written something not as elegant as the above several times in the past when rendering tables in Rails. 在Rails中渲染表时,我曾经多次写过一些不像上面那样优雅的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM