- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Ruby 中有一个“super”关键字,它正在查看祖先链,以便找到链上的第一个方法实现并执行它。所以,这就是它在 Ruby 中的工作方式,不足为奇:
module Mammal
def walk
puts "I'm walking"
end
end
require '~/Documents/rubytest/super/mammal.rb'
class Cat
include Mammal
def walk
super
end
end
2.7.0 :001 > simba = Cat.new
2.7.0 :002 > simba.walk
I'm walking
=> nil
这是理想的行为。现在,在 Rails 中有 ActiveSupport::Concern 为模块提供了一些额外的功能。如果您使用 ActiveSupport 帮助器以类似的方式进行某种,会发生以下情况:
module MammalMixin
extend ActiveSupport::Concern
included do
def show
@mammal = Mammal.find(params[:id])
end
end
end
class SomeController < ApplicationController
include MammalMixin
def show
super
end
end
如果您到达该 Controller ,则会出错:super:#SomeController:0x000055f07c549bc0 没有父类(super class)方法“show”
当然,可以不使用“included do”帮助器并恢复为普通的 Ruby 样式,但有人可以建议 ActiveSupport::Concern 中究竟是什么阻止“super”正常工作并(也许)解释背后的基本原理这个?
我一直在查看 active_support/concern.rb 中的源代码,但没看懂。
最佳答案
答案就在the documentation of ActiveSupport::Concern#included
[粗体强调我的]:
Evaluate given block in context of base class, so that you can write class macros here.
所以,这里是你的 block 的内容:
def show
@mammal = Mammal.find(params[:id])
end
根据文档,在基类的上下文中评估此 block 。现在,当你评估一个方法时会发生什么def
类上下文中的初始表达式?你在那个类中定义一个方法!
所以,你在这里所做的是定义一个名为 show
的方法。在 SomeController
就像你写的一样上课:
class SomeController < ApplicationController
def show
@mammal = Mammal.find(params[:id])
end
def show
super
end
end
换句话说,您的第二个定义是overwriting the first definition, not overriding it ,所以没有 super 方法。
正确的方法使用ActiveSupport::Concern#included
是这样的:
module MammalMixin
extend ActiveSupport::Concern
def show
@mammal = Mammal.find(params[:id])
end
included do
acts_as_whatever
end
end
ActiveSupport::Concern#included
,正如文档所说,用于在类的上下文中执行代码(例如“类宏”,如 acts_as_*
、 has_many
、 belongs_to
等)。
Here's how including a module normally works :
当你写作时
class C
include M
end
您调用 Module#include
方法(它不会被 Class
覆盖,因此不会更改地继承)。
现在,Module#include
实际上并没有做任何有趣的事情。它基本上看起来像这样:
class Module
def include(mod)
mod.append_features(self)
end
end
这是一个经典的 Double Dispatch 习惯用法,可以让模块完全控制它希望如何被包含到类中。当你打电话时
C.include(M)
这意味着 C
处于控制之中,它只是委托(delegate)给
M.append_features(C)
其中包含 M
控制。
什么 Module#append_features
通常是这样的(我会用伪 Ruby 来描述它,因为在 Ruby 中无法解释这种行为,因为必要的数据结构是引擎内部的):
class Module
def append_features(base)
if base.is_a?(Module)
base.included_modules << self unless base.included_modules.include?(self)
else
old_superclass = base.__superclass__
klazz = Class.new(old_superclass)
klazz.__constant_table__ = __constant_table__
klazz.__class_variable_table__ = __class_variable_table__
klazz.__instance_variable_table__ = __instance_variable_table__
klazz.__method_table__ = __method_table__
klazz.__virtual__ = true
base.__superclass__ = klazz
end
included(base)
self
end
end
那么,Ruby 创建了一个新类,称为包含类,它的常量表指针、类变量表指针、实例变量表指针和方法表指针都指向常量表、模块的类变量表、实例变量表、方法表。基本上,我们正在创建一个隐藏模块的类。
然后它使这个类成为该类的新父类(super class),并使旧父类(super class)成为包含类的父类(super class)。实际上,它将类和父类(super class)之间的包含类插入到继承链中。
这样做是因为 the method lookup algorithm doesn't need to know anything about mixins and can be kept very simple :去类,检查方法是否存在,如果不获取父类(super class),检查方法是否存在,依此类推。由于方法查找是面向对象语言执行引擎中最常见和最重要的操作之一,因此算法的简单和快速至关重要。
这个 include 类将被 Class#superclass
跳过方法,所以你看不到它,但它会显示为 Module#ancestors
.
这就是为什么super
有效:因为模块字面意思变成了父类(super class)。
我们从 C < Object
开始我们最终得到 C < M' < Object
.
现在, ActiveSupport::Concern
完全搞砸了。
ActiveSupport::Concern#included
method 的有趣部分这是:
@_included_block = block
它只是存储 block 供以后使用。
如上所述,当 MammalMixin
被纳入 SomeController
,即当 SomeController.include(MammalMixin)
被调用,SomeController.include
(即 Module#include
)将依次调用 MammalMixin.append_features(SomeController)
. MammalMixin.append_features
在这种情况下是 ActiveSupport::Concern#append_features
,最有趣的部分是:
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
如您所见,它使用的是 Module#class_eval
为了评估它之前在包含它的基类的上下文中保存的 block 。这就是使您的方法最终成为基类而不是模块的实例方法的原因。
关于ruby-on-rails - 为包含的方法调用 super 会导致 "no superclass method"错误 - ActiveSupport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62579411/
sanitize 是什么意思在 Rails 中是什么意思? 我正在阅读 CanCanCan 的文档.它说: When using strong_parameters or Rails 4+, you
在过去的几个月里,我感觉自己对 Ruby on Rails (RoR) 开发的了解达到了极限。我为大/小客户和 friend /爱好项目开发了大大小小的应用程序。我知道如何开发这些应用程序,但开始感觉
我昨天参加了一个关于扩展 Rails 的聚会,其中一个主题是 Hexagonal Rails。然而,我只做了一年的 Rails,对 MVC 结构非常满意(也许太舒服了),所以我不太了解适配器和消息队列
我使用多个 Rails 应用程序,一些在 Rails 3.2/Ruby 2.0 上,一些在 Rails 2.3/Ruby 1.8.7 上。 他们的共同点是,随着他们的成长和添加更多的依赖项/ gem
这个问题在这里已经有了答案: Using Rails-UJS in JS modules (Rails 6 with webpacker) (5 个答案) 关闭 3 年前。 我正在尝试使用 UJS
我正在开发一个当前使用 Rails 1.2 的 Rails 应用程序,所以我现在离最新的稳定版本(Rails 2.3)还有很长的路要走。 我应该如何进行迁移到更新版本的 Rails 的过程? 我应该一
尝试按照 Ryan Bates Backbone.js 教程构建抽奖应用程序,但我已经遇到了第一段代码的问题。在 application.js 的 init 函数中,他初始化了 Raffler 路由的
我正在使用 Rails 3.2 并且我有一个数据库表,我想在其中找到符合以下条件的所有行: a = true and b = true and ( 0 true, :b =>
我有一个用户类和一个联系人,其中联系人是用户的子类。这两个类都存储在用户表中。 我的联系人可能有也可能没有电子邮件地址,而我的用户需要一个电子邮件地址(我的用户模型定义中有 validates_pre
我正在编写一个教程,我在其中演示了一些 rails 命令。在我的机器上 rails和 script/rails两者都同样有效。有“首选”形式吗?两者中哪一个更普遍? 最佳答案 当您运行 rails 时
我正在寻找有关通过我的应用程序前进的最佳方式的建议,这是我首次开始集成Elasticsearch。我是一名初学者,但是热衷于深入研究,以便原谅任何明显的错误! 我遵循了http://www.sitep
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我有 Rails 4,这是我的默认版本(我仍然希望它是)。但我不想在我的电脑上添加 rails 3.2。在以下命令中:gem install rails -v 3.2.16 我有这个警告: railt
您好,我想使用 Sheevaplug 构建一个“Rails Brick”来自 Marvell(操作系统是开箱即用的 Ubuntu,但您可以在其上安装其他发行版)。它将成为家庭服务器和静音、低成本(99
我需要能够从 Rails 控制台发送我的 Rails 应用程序的 Postgres 数据库中所有未接受的邀请。 (我有一个名为 Invitations 的表,其中包含一个名为 accepted 的 b
validate :cannot_modify_if_locked, on: :update def cannot_modify_if_locked if self.locked erro
我正在学习教程(学习 Rails 播客),需要更改以下路由语法,以便它与 Rails 3.0 兼容。谁能帮忙? map.view_page ':name', :controller => 'viewe
我是一名优秀的程序员,十分优秀!