我是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 endWhat is the difference between doing this:
ActiveSupport::Notifications.instrument(event_name, payload) do yield endversus doing this:
ActiveSupport::Notifications.instrument(event_name, payload) yieldIf 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 eventyield的使用允许它用一些定时代码包装代码的执行。 在您的第二个示例中,没有块传递给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 ... endChecks 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 eventThe 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
更多推荐
发布评论