问题描述
由于教程,我想我了解触发器的工作原理,但那里的示例仅用于教学.任何人都可以举一个例子说明你如何实际使用或将使用触发器?
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?
该链接曾经是 http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators
,但现在似乎是垃圾邮件.
The link used to be http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators
, but seems to be spam this days.
推荐答案
这是一个例子(取自 rubycentral.com 文章),您可以在其中仅打印文件中的某些行:
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
程序只会打印出来:
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.
这篇关于Ruby 触发器什么时候有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!