不是关键字,不在 Ruby-Doc 中 include 和 extends 是哪种语言结构?
除非我错过了这些条目,否则我无法在 Ruby-Doc.org 文档或 Ruby 的关键字列表中找到 include 或extend。我可以找到 obj.extend,但找不到类定义中使用的扩展语句,例如
# add module Foo's methods as class methods of TestClass
class TestClass
extend Foo
end
或
# add module Foo's methods as instance methods of TestClass
class TestClass
include Foo
end
作为语言构造,如果这是正确的术语,那么 include 和 extends 是什么,它们在哪里定义?
如果能够了解这两个语句在 Ruby 语言中是如何实现的,那就太好了。谢谢。
Unless I've missed the entries, I can't find include or extend in the Ruby-Doc.org documentation or in Ruby's keyword list. I can find obj.extend but not the extend statement used in a class definition e.g.
# add module Foo's methods as class methods of TestClass
class TestClass
extend Foo
end
or
# add module Foo's methods as instance methods of TestClass
class TestClass
include Foo
end
As a language construct, if that's the proper term, what are include and extend and where are they defined?
It would be nice to understand just how these two statements are implemented in the Ruby language. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
extend
是一个Object
方法,extend_object
的便捷方法(或多或少)。include
位于Module
中,一个包装器围绕append_features
。extend
is anObject
method, a convenience method forextend_object
(more or less).include
is inModule
, a wrapper aroundappend_features
.include是Module类的私有方法,extend是Object类中的方法
include is a private method of the Module class and extend is a method in the Object class