ruby - 调用 `super` 的时机
问题描述
我应该理解这段代码:
module Stealth
module MixpanelSessionTracking
def set(flow:, state:)
retval = super
if ENV['MIXPANEL_PROJECT_TOKEN'].present?
mixpanel = Stealth::Mixpanel.new
mixpanel.tracker.track(user_id, 'State Change', {
'flow' => flow,
'state' => state
})
end
retval
end
end
class Session
prepend Stealth::MixpanelSessionTracking
end
end
为什么我设置super
为变量,并且在方法定义结束时setval
调用变量之前?retval
我可以super
在方法定义的末尾使用吗?
编辑:
前身类方法:
module Stealth
class Session
(...)
def set(flow:, state:)
store_current_to_previous(flow: flow, state: state)
@flow = nil
@session = canonical_session_slug(flow: flow, state: state)
Stealth::Logger.l(topic: "session", message: "User #{user_id}: setting session to #{flow}->#{state}")
$redis.set(user_id, session)
end
(...)
end
end
解决方案
首先,你需要看到ancestors
你的班级会像 [Stealth::MixpanelSessionTracking, Session] 因为你使用prepend
.
然后你set
首先调用 Stealth::MixpanelSessionTracking 和 Session 类的超级内部set
方法,因为祖先链带有前置。
推荐阅读
- python - 如何使用python从结束日期时间中减去开始日期时间?
- caching - 升级到 TYPO3 v. 10.4 后缓存不稳定
- python - Python 2.7 HTTP 基本身份验证
- google-cloud-platform - 将 IAM 用户限制在 GCP 中的特定区域和实例类型/类
- python - Python:散列大量大数
- typescript - .map() 上的联合类型的字符串 [] | string[][] 导致错误
- .net - 有没有办法对我的自定义“dotnet new”模板中的参数进行正则表达式检查?
- python - 获取当前的 jupyter-lab 笔记本名称 [适用于 Jupyter-lab 版本 2.1 和 3.0.1 以及笔记本版本 >6.0.3)
- istio - Istio 没有将流量路由到特定的 pod
- flutter - 如何在 Flutter App 上的 OSM 上绘制路线