ruby - 在 Ruby 中调用超级模块中的重写方法
问题描述
在以下代码中,Parent#just_do
覆盖GrandParent#just_do
. 在Me
课堂上,我该如何打电话GrandParent#just_do
?
module GrandParent
def just_do
puts "GrandParent"
end
end
module Parent
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
include GrandParent
def just_do
puts "Parent"
end
end
end
class Me
include Parent
def self.just_do
super # this calls Parent#just_do
end
end
解决方案
您可以直接使用方法对象。
class Me
include Parent
def self.just_do
method(:just_do).super_method.super_method.call
end
end
推荐阅读
- javascript - JSON stringify将完整的数组元素转换为PHP中数组的键
- rxjs - 带有 vue-rx / rxjs 的 VUE:如何使用过滤器和间隔创建带有 $watchAsObservable 的订阅
- c# - 如何在 C# .net 中对包含带有字母和数字的字符串的字符串列表进行降序排序?
- c# - 如何使用 Mathf.Lerp 更改对象的平滑位置?
- reactjs - 如何在 React Native 中使用 fetch
- javascript - 如何根据 Angular 中的值显示/隐藏项目?
- javascript - 是否有任何方法可以解决顶点图表中的标记问题
- floating-point - “p”在浮点数的指数十六进制表示法中代表什么
- upgrade - 当 Storm 客户端从 0.10 升级到 1.1.0 时,Storm 拓扑部署失败并出现 IllegalArgumentException
- php - 如何将其他数据传递给 Laravel Socialite 中的回调方法?