Ruby 触发器什么时候有用?

发布于 2024-07-26 15:40:13 字数 234 浏览 7 评论 0原文

我想我通过教程了解了触发器的工作原理,但那里的示例只是为了教学而设计的。 谁能举例说明您如何实际使用或将使用触发器?

我正在寻找一个真实世界的应用程序,而不仅仅是另一个演示。 这个工具能解决什么问题?

该链接曾经是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫离伤花 2024-08-02 15:40:13

这是一个示例(取自 rubycentral.com 文章),您仅打印出以下内容中的某些行:一个文件:

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

假设您有一个包含以下内容的文件:

first
second
third
fourth
fifth
sixth

程序只会打印出:

third
fourth
fifth

这个想法是,该值在左侧事件发生之前为 true,然后在右侧事件发生之前保持为 true 。 如果使用得当,这可能是一个很好的语法糖,但你需要小心,使内容可读。

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.

懵少女 2024-08-02 15:40:13

我想用一些具体的例子来补充詹姆斯的回答。 我使用此运算符根据正则表达式提取文本部分。

我正在编写一个工具,涉及通过 Net::SSH 在远程服务器上运行命令。 这个特定的服务器有一个恼人的习惯,无论会话是否是登录会话,都会打印 MOTD。 当我运行命令并检索输出时,这导致返回大量垃圾。 由于我对服务器设置没有太多影响,因此我创建了一个小脚本来打印一个分隔符,运行该程序,然后打印另一个分隔符。 输出看起来像这样。

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
<<<<<<<<<<<<<<<<<<<<<<<<<

触发器运算符是一个有用的快捷方式,可以仅提取具有我需要的输出的代码部分。 我使用了一个匹配 25 个大于号“>”的正则表达式 开始比赛,25个小于“<” 结束比赛。

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

输出

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

我见过的大多数示例都涉及根据正则表达式从文件或数组中提取数据块。 我想到的其他一些示例包括排除 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.

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.

狠疯拽 2024-08-02 15:40:13

在具有多行的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文