Ruby 触发器什么时候有用?
我想我通过教程了解了触发器的工作原理,但那里的示例只是为了教学而设计的。 谁能举例说明您如何实际使用或将使用触发器?
我正在寻找一个真实世界的应用程序,而不仅仅是另一个演示。 这个工具能解决什么问题?
该链接曾经是http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators
,但现在似乎是垃圾邮件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个示例(取自 rubycentral.com 文章),您仅打印出以下内容中的某些行:一个文件:
假设您有一个包含以下内容的文件:
程序只会打印出:
这个想法是,该值在左侧事件发生之前为 true,然后在右侧事件发生之前保持为 true 。 如果使用得当,这可能是一个很好的语法糖,但你需要小心,使内容可读。
Here's an example (taken from a rubycentral.com article) where you print out only certain lines from a file:
This assumes that you have a file with the following contents:
The program will only print out:
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.
我想用一些具体的例子来补充詹姆斯的回答。 我使用此运算符根据正则表达式提取文本部分。
我正在编写一个工具,涉及通过 Net::SSH 在远程服务器上运行命令。 这个特定的服务器有一个恼人的习惯,无论会话是否是登录会话,都会打印 MOTD。 当我运行命令并检索输出时,这导致返回大量垃圾。 由于我对服务器设置没有太多影响,因此我创建了一个小脚本来打印一个分隔符,运行该程序,然后打印另一个分隔符。 输出看起来像这样。
触发器运算符是一个有用的快捷方式,可以仅提取具有我需要的输出的代码部分。 我使用了一个匹配 25 个大于号“>”的正则表达式 开始比赛,25个小于“<” 结束比赛。
输出
我见过的大多数示例都涉及根据正则表达式从文件或数组中提取数据块。 我想到的其他一些示例包括排除 git merge 冲突、遗留平面文件系统中的某些记录(例如写入文件的结构)和日志文件。
基本上,任何时候您都需要根据开始和结束数据提取部分,而不仅仅是单个行的内容。 它比简单的正则表达式要复杂一些,但比编写解析器要简单一些。
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.
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
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.
在具有多行的 HTML 表格中突出显示奇数/偶数行似乎是一个有效的用例。
过去在 Rails 中渲染表格时,我曾多次写过一些不如上面的优雅的东西。
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.