![](/img/trans.png)
[英]RoR - how can I refer to 'old' self object when self's context changes inside a method
[英]Can I use the context as @context inside an interactor class?
在某些交互器中,定義了幾個函數以在調用 function 中使用(我沒有使用實用程序來組織此類函數)。
在這種情況下使用@context
而不是context
可以嗎?
參考 URL: https://github.com/collectiveidea/interactor
謝謝你的幫助
從源代碼context
中可以看出,它只是attr_reader
聲明的一個簡單的訪問器(getter 方法):
module Interactor
# Internal: Install Interactor's behavior in the given class.
def self.included(base)
base.class_eval do
extend ClassMethods
include Hooks
# Public: Gets the Interactor::Context of the Interactor instance.
attr_reader :context
end
end
因此,只要您從包含此模塊的 class 中訪問實例變量,直接訪問實例變量 ( @context
) 和通過context
方法幾乎沒有明顯區別。
class MyInteractor
include Interactor
def my_method
@context
# is the exact same as
context
# except for the method call
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.