这个 Rails 引擎代码是什么意思:config.to_prepare &method(:activate).to_proc [英] What does this Rails Engine code mean: config.to_prepare &method(:activate).to_proc

查看:51
本文介绍了这个 Rails 引擎代码是什么意思:config.to_prepare &method(:activate).to_proc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用广泛使用 Rails 引擎的 Spree.我发现的一个常见模式是这样的.但是我无法理解这段代码.

I'm working with Spree, which uses Rails engines extensively. A common pattern I find is this. However I have trouble understanding this code.

class Engine < Rails::Engine 
  def self.activate
    ...
  end     

  config.to_prepare &method(:activate).to_proc
end

  1. .to_prepare 有什么作用
  2. &method 运算符有什么作用?
  3. 代码的整体效果如何?
  1. What does .to_prepare do
  2. What does the &method operator do?
  3. What is the overall effect of the code?

非常感谢!

推荐答案

&method(:activate).to_proc

最好一次食用一件.(应该注意,这部分代码是 100%-Rails 特定的 Ruby.)

This is best consumed one item at a time. (It should be noted that this portion of the code is 100% non-Rails-specific Ruby.)

method 是一个 Ruby 方法(它都是非常元的).它如此记录:

method is a Ruby method (it's all very meta). It is documented thusly:

查找命名方法作为obj 中的接收者,返回一个方法对象(或引发 NameError).这方法对象作为一个闭包obj 的对象实例,所以实例变量和 self 的值保持不变可用.

Looks up the named method as a receiver in obj, returning a Method object (or raising NameError). The Method object acts as a closure in obj’s object instance, so instance variables and the value of self remain available.

查看以下 IRB 会议:

Check out the following IRB session:

ruby-1.9.2-p136 :001 > def my_method
ruby-1.9.2-p136 :002?>   puts "in my_method"
ruby-1.9.2-p136 :003?>   10
ruby-1.9.2-p136 :004?>   end
 => nil 
ruby-1.9.2-p136 :005 > method(:my_method)
 => #<Method: Object#my_method>

因此 method 调用正在查找(在您的示例中)activate 方法并创建一个 Method 对象.Method 包含一个名为 to_proc 的方法,它返回与此方法对应的 Proc 对象".继续我们的 IRB 会议:

So the method call is looking up (in your example) the activate method and creating a Method object for it. Method contains a method called to_proc, which "returns a Proc object corresponding to this method". Continuing our IRB session:

ruby-1.9.2-p136 :006 > method(:my_method).to_proc
 => #<Proc:0x000001010a3e38 (lambda)>

最后,我们使用与号运算符,当在方法调用期间位于 Proc 对象之前时,它将被 Proc 包含的块替换.再来一次 IRB:

Finally, we use the ampersand operator, which, when preceding a Proc object during a method call, will be replaced by the block the Proc contains. One more time in IRB:

ruby-1.9.2-p136 :007 > def executor(&block)
ruby-1.9.2-p136 :008?>   block.call
ruby-1.9.2-p136 :009?>   end
 => nil 
ruby-1.9.2-p136 :010 > executor( &method(:my_method).to_proc )
in my_method
 => 10

因此,在伪代码中,您列出的行是说:

So, in pseudocode, what the line you listed is saying is:

config.to_prepare(a block containing the functionality of the method activate)

config.to_prepare

[根据 schof 的评论编辑]

config.to_prepare 需要一个应该运行的块来设置你的 Railtie/引擎.它在生产模式和开发中的每个请求上运行一次,并且是保证在开发模式下对每个请求调用的唯一代码.如果您正在修改类(class_eval 等)作为引擎初始化的一部分,这一点很重要;否则,您将在开发和生产中看到不同的行为.

config.to_prepare takes a block that should be run to set up your Railtie/Engine. It is run once in production mode and on every request in development, and is the only code guaranteed to be called on every single request in development mode. This is important if you are modifying classes (class_eval, etc.) as part of your engine initialization; otherwise, you're going to see different behavior in development vs. production.

为什么?

这是一个常见习惯用法的原因是,您不必在 to_prepare 块本身内定义准备代码;您可以在类中定义方法,然后使用上述魔法将它们转换为块(记住其作用域).该代码在功能上等同于:

The reason this is a common idiom is so that you don't have to define your preparation code inside the to_prepare block itself; you can define method(s) in your class and then convert them to a block (which remembers its scope) using the magic described above. The code is functionally equivalent to:

class Engine < Rails::Engine 
  config.to_prepare do
    (the contents of self.activate)
  end
end

这篇关于这个 Rails 引擎代码是什么意思:config.to_prepare &amp;method(:activate).to_proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆