在Ruby中,为什么要在你正在进行的通话中包含“收益”呢?(In Ruby, why wrap “yield” in a call you are making anyway?)

编程入门 行业动态 更新时间:2024-10-10 14:23:59
在Ruby中,为什么要在你正在进行的通话中包含“收益”呢?(In Ruby, why wrap “yield” in a call you are making anyway?)

我是Ruby的新手。 我在这里读到的东西让我感到困惑:

http://alma-connect.github.io/techblog/2014/03/rails-pub-sub.html

他们提供以下代码:

# app/pub_sub/publisher.rb module Publisher extend self # delegate to ActiveSupport::Notifications.instrument def broadcast_event(event_name, payload={}) if block_given? ActiveSupport::Notifications.instrument(event_name, payload) do yield end else ActiveSupport::Notifications.instrument(event_name, payload) end end end

这样做有什么区别:

ActiveSupport::Notifications.instrument(event_name, payload) do yield end

与此相反:

ActiveSupport::Notifications.instrument(event_name, payload) yield

如果这是另一种语言,我可能会假设我们首先调用方法instrument() ,然后调用yield以调用块。 但这不是他们写的。 它们显示嵌套在ActiveSupport::Notifications.instrument()的ActiveSupport::Notifications.instrument() 。

我应该假设ActiveSupport::Notifications.instrument()返回某种迭代,我们将迭代? 我们是否为ActiveSupport::Notifications.instrument()返回的每个项调用一次ActiveSupport::Notifications.instrument() ?

I am new to Ruby. I am confused by something I am reading here:

http://alma-connect.github.io/techblog/2014/03/rails-pub-sub.html

They offer this code:

# app/pub_sub/publisher.rb module Publisher extend self # delegate to ActiveSupport::Notifications.instrument def broadcast_event(event_name, payload={}) if block_given? ActiveSupport::Notifications.instrument(event_name, payload) do yield end else ActiveSupport::Notifications.instrument(event_name, payload) end end end

What is the difference between doing this:

ActiveSupport::Notifications.instrument(event_name, payload) do yield end

versus doing this:

ActiveSupport::Notifications.instrument(event_name, payload) yield

If this were another language, I might assume that we first call the method instrument(), and then we call yield so as to call the block. But that is not what they wrote. They show yield being nested inside of ActiveSupport::Notifications.instrument().

Should I assume that ActiveSupport::Notifications.instrument() is returning some kind of iterable, that we will iterate over? Are we calling yield once for every item returned from ActiveSupport::Notifications.instrument()?

最满意答案

虽然块经常用于迭代,但它们还有许多其他用途。 例如,一个是确保适当的资源清理

ActiveRecord::Base.with_connection do ... end

检查线程的数据库连接,产生块然后再检查连接。

在仪器方法的特定情况下,您发现它所做的是添加到事件数据中,它将广播有关块执行时间的信息。 实际的实施更复杂,但从广义上讲,它并没有那么不同

event = Event.new(event_name, payload) event.start = Time.now yield event.end = Time.now event

yield的使用允许它用一些定时代码包装代码的执行。 在您的第二个示例中,没有块传递给instrument , instrument检测到这一点并将其记录为没有持续时间的事件

While blocks are frequently used for iteration they have many other uses. One is to ensure proper resource cleanup, for example

ActiveRecord::Base.with_connection do ... end

Checks out a database connection for the thread, yields to the block and then checks the connection back in.

In the specific case of the instrument method you found what it does is add to the event data it is about to broadcast information about the time it's block took to execute. The actual implementation is more complicated but in broad terms it's not so different to

event = Event.new(event_name, payload) event.start = Time.now yield event.end = Time.now event

The use of yield allows it to wrap the execution of your code with some timing code. In your second example no block is passed to instrument, which detects this and will record it as an event having no duration

更多推荐

本文发布于:2023-08-07 11:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要在   正在进行   收益   Ruby   making

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!