`
tim.teng
  • 浏览: 9963 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Including a Module with append_features

阅读更多

Including a Module with append_features

module MyMod

def MyMod.append_features(someClass)
def someClass.modmeth
puts "Module (class) method"
end
super # This call is necessary!
end

def meth1
puts "Method 1"
end

end


class MyClass

include MyMod

def MyClass.classmeth
puts "Class method"
end

def meth2
puts "Method 2"
end

end


x = MyClass.new

# Output:
MyClass.classmeth # Class method
x.meth1 # Method 1
MyClass.modmeth # Module (class) method
x.meth2 # Method 2

This example is worth examining in detail. First, we should understand that append_features isn't just a hook that is called when an include happens; it actually does the work of the include operation. That's why the call to super is needed; without it, the rest of the module (in this case, meth1) wouldn't be included at all.

Also note that within the append_features call, there is a method definition. This looks unusual, but it works because the inner method definition is a singleton method (class-level or module-level). An attempt to define an instance method in the same way would result in a Nested method error.

Conceivably a module might want to determine the initiator of a mixin. The append_features method can also be used for this because the class is passed in as a parameter.

It is also possible to mix in the instance methods of a module as class methods. 

module MyMod

def meth3
puts "Module instance method meth3"
puts "can become a class method."
end

end


class MyClass

class << self # Here, self is MyClass
include MyMod
end

end


MyClass.meth3

# Output:
# Module instance method meth3
# can become a class method.

The extend method is useful here. This example simply becomes:

class MyClass
extend MyMod
end
评论

相关推荐

    PyPI 官网下载 | fast_append_array-0.1.0.tar.gz

    "fast_append_array-0.1.0.tar.gz"是一个从PyPI官网下载的压缩包,很可能包含了名为`fast_append_array`的Python库,版本号为0.1.0。这个库可能是为了优化数组或列表的追加操作,以提高性能。 Python中的数组操作是...

    append_values.py

    append_values

    python-django中的APPEND_SLASH实现方法

    Return True if settings.APPEND_SLASH is True and appending a slash to the request path turns an invalid path into a valid one. """ if settings.APPEND_SLASHand not request.path_info.endswith('/'): ...

    PHP中auto_prepend_file与auto_append_file用法实例分析

    如果不需要对所有页面都使用auto_prepend_file和auto_append_file,可以针对特定目录使用.htaccess文件来进行设置。在.htaccess文件中,可以使用php_value指令来设置auto_prepend_file和auto_append_file: ...

    遍历文件夹中mdb合并到一个GDB中 120200220103945_mdbgdb合并Append_

    "遍历文件夹中mdb合并到一个GDB中 120200220103945_mdbgdb合并Append_" 这个任务就涉及到这样的操作,通过Python脚本自动化执行。 在GIS领域,mdb和GDB都是用于存储地理空间数据的标准格式。mdb是早期的Access...

    new_append_passwd.py

    通过此部分小程序可实现通过代码往excel表格中记录一些个人重要的内容。只需按照提示输入内容即可,不用再人工找到表格,打开,写入,保存,关闭等。

    mod_jk-1.2.26-httpd-2.2.4.rar

    4. 修改Apache的配置文件`httpd.conf`,加载mod_jk模块并配置相关指令,如`LoadModule jk_module /path/to/mod_jk-1.2.26-httpd-2.2.4.so`。 5. 配置`workers.properties`文件,定义Tomcat服务器的实例和负载均衡...

    O_APPEND与读操作

    本主题聚焦于`O_APPEND`标志和`lseek`函数在文件访问中的行为,特别是它们如何影响读写操作。 `O_APPEND`是一个在`open`函数中使用的标志,用于指定打开文件的方式。如果设置了这个标志,每次对文件进行写操作(如`...

    append name_picproteus_

    标题中的"append name_picproteus_"似乎是在指示一个与PIC微控制器和Proteus相关的编程操作,其中"append name"可能是指向一个文件或变量添加特定的名称或标识符,而"_picproteus_"可能是这个过程的一个特定标记或者...

    fast_append_array:轻量级的Python DataFrame具有快速的附加操作

    快速追加数组一个数据框...原始存储库: : 作者:Marius ( )变更日志 0.2.0将append_dict()的速度提高5倍元素访问和切片的小改进支持不同的dtypes from_pandas()和from_dicts()函数 0.1.0初版执照麻省理工学院-请参阅

    cef_binary_3.2358 windows32

    cef3是支持多前程的google浏览器官方内核代码,其中有各种实例 (cef3 support multiple future google browser official kernel code, including various instances)

    moviedb-promise:现在可以在TypeScript中与themoviedb.org的api节点进行交互!

    应该将append_to_response添加到适当功能的请求参数中, append_to_response应该将其添加到选项中 每个函数的最后一个参数可以是axios配置对象,它将覆盖基础请求上的所有内容。 几个功能已被重命名。 搜索函数...

    支持中文目录wget版本,linux版本

    append_uri_pathel()函数是通过FILE_CHAR_TEST (*p, mask)这一句来判断该字符是否为特殊字符,而同时它会认为中文也是特殊字符,然后按照转换空格之类的方式对中文进行转义,这样就会造成中文乱码的情况,知道了问题...

    config.vendor

    uclinux2.4移植s3c44b0时需要的用户应用配置文件,可以直接加载,已经做了符合目标板的配置修改,问题不大

    jstree json_data异步加载实例

    struts2 jstree json_data实现异步加载 定位

    c#调用Python源码,No module named xx解决方案

    - **调整sys.path**: 在Python代码中,可以使用`sys.path.append()`添加模块所在的路径,使其能被Python解释器找到。 - **打包模块**: 如果模块是自定义的,可以将其打包成`.egg`或`.whl`文件,然后使用`pip`安装...

    php实现的xml操作类

    分享给大家供大家参考,具体如下: &lt;?.../* 使用方法: $test=new xml(); $test-&gt;new_xml('test.xml');...$test-&gt;root('document');...$test-&gt;append_root_node('book');...$test-&gt;append_child_node('...$test-&gt;append_roo

    Python定义函数时参数有默认值问题解决

    这篇文章主要介绍了Python定义函数时参数有默认值问题解决,文中通过示例代码介绍的非常详细,对...def append_item(item, list1=[]): list1.append(item) return list1 print(append_item((1))) # [1] print(append

Global site tag (gtag.js) - Google Analytics