当前位置: 代码网 > it编程>前端脚本>Ruby > Ruby 中的 module_function 和 extend self异同

Ruby 中的 module_function 和 extend self异同

2024年05月15日 Ruby 我要评论
在阅读开源的 ruby 代码和编写可维护性的代码经常遇到这两者的使用,那么他们两者的共同点和区别是什么呢?module_functionruby 的 module 是 method 和 constan

在阅读开源的 ruby 代码和编写可维护性的代码经常遇到这两者的使用,那么他们两者的共同点和区别是什么呢?

module_function

ruby 的 module 是 method 和 constants 的集合。module 中的method 又可分为 instance method 和 module method, 当一个 module 被 include 进一个 class ,那么 module 中的 method (注:没有被 module_function 标记的 method)就是 class 中的 instance method, instance method 需要所在的 class 被实例化之后才能被调用;被 module_function 标记的 method(不管该 method 是 public 或者 private)就是 module method 且 instance method 也会变成 private method,对于被 include 所在的 class 来说是 private method,object.module_name 会出错。module method 都能被 module_name.method_name 调用,没有被 module_function 标记的 public method 不能被 module_name.method_name 调用。

module 中的 module_function 会把 module 中的 method 变成 module method 且对于被 include 所在的 class 来说,module method 在 module 中是 private method 故 module_name.module_method 能调用,而不能被 object.module_name 调用。

module 中的 public method 对于被 include 所在的 class 来说是 instance method,故 object.public_method_in_module 能调用。如果想要非 module method 能够被 module 调用(module_name.not_module_method) ,需要引入 extend self (下文会讨论 extend self)

# test.rb
module mymodule
 def public_meth
  p "a public method, if the module is included to a class , can be call as object.public_meth"
 end
 def module_method
  p "a module method,can be called as module_name.module_method. but can not be call as object.module_method"
 end
 private
 def private_method_to_module_function
  p "a private_method, but can be call as module_name.module_method, because it was assigned to module_function"
 end
 def private_method
  p "i am a private method"
 end
 module_function :module_method, :private_method_to_module_function
end

mymodule.module_method
mymodule.private_method_to_module_function
begin
 mymodule.public_meth
rescue
 p "public method can not be called by module_name.public_meth"
end
begin
 mymodule.private_method
rescue nomethoderror
 p "private method can not be called by module_name.module_method"
end

class myclass
 include mymodule
end

obj = myclass.new
obj.public_meth

begin
 obj.private_method
rescue nomethoderror
 p "private method in module can not be call by object.method_name"
end

begin
 obj.module_method
rescue nomethoderror
 p "module method can not be called by object.method_name, for object, module method is private instance method"
end

#调用
ruby test.rb
"a module method,can be called as module_name.module_method. but can not be call as object.module_method"
"a private_method, but can be call as module_name.module_method, because it was assigned to module_function"
"public method can not be called by module_name.public_meth"
"private method can not be called by module_name.module_method"
"a public method, if the module is included to a class , can be call as object.public_meth"
"private method in module can not be call by object.method_name"
"module method can not be called by object.method_name, for object, module method is private instance method"

总结就是

•the method will be copied to class' singleton class
•the instance method's visibility will become private

extend self

include is for adding methods to an instance of a class and extend is for adding class methods

extend 本质是给 class 或者 module 添加 class method

extend self 让 module 中的 instance method 能够被 module_name.instance_method 调用,保留 module 中原本 method 的 public 或 private 属性,但又不像 module_function 一样把被标记的 method 变成 private 。

#!/usr/bin/env ruby
# encoding: utf-8
# test_extend.rb
module mymodule
 extend self
 def public_meth
  p "a public_method extended by self can be called by module_name.public_meth and object.public_meth, included by a class"
  private_method
 end
 private
 def private_method
  p "a private method, can be call in module internal"
 end
end

class myclass
 include mymodule
end

mymodule.public_meth

begin
 mymodule.private_method
rescue nomethoderror
 p "private method in extend self module can not be called module_name.private_method"
end

obj = myclass.new
obj.public_meth

begin
 obj.private_method
rescue nomethoderror
 p "private method can not be called by object.private_method"
end

# 调用 ruby test_extend.rb
"a public_method extended by self can be called by module_name.public_meth and object.public_meth, included by a class"
"a private method, can be call in module internal"
"private method in extend self module can not be called module_name.private_method"
"a public_method extended by self can be called by module_name.public_meth and object.public_meth, included by a class"
"a private method, can be call in module internal"
"private method can not be called by object.private_method"

总结就是:
•no method copying involved
•no changes to method visibility

总结

module_function 改变 module 内 原来 method 的 public/private 属性并把改 method 变成 module method ,能够被 module_name.module_method 调用。

extend self 就是在 module 自继承,不改变 module 中 method 的 public/private 属性,能够被 module_name.public_method

(0)

相关文章:

  • redis集群搭建教程及遇到的问题处理

    这里,在一个linux虚拟机上搭建6个节点的redis伪集群,思路很简单,一台虚拟机上开启6个redis实例,每个redis实例有自己的端口。这样的话,相当于模拟出了6台机器了,然…

    2024年05月15日 前端脚本
  • Ruby实现的图片滤镜算法代码

    原图一、灰度算法彩色照片每一个像素的颜色值由红、绿、蓝三种值混合而成,红绿蓝的取值分别由很多种,于是像素的颜色值也可以有很多种颜色值,这就是彩色图片的原理,而灰度照片则只有256种…

    2024年05月15日 前端脚本
  • ruby中并发并行与全局锁详解

    ruby中并发并行与全局锁详解

    前言本文主要给大家介绍了关于ruby并发并行和全局锁的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。并发和并行在开发时,我们经常会接... [阅读全文]
  • ruby 正则表达式详解及示例代码

    ruby 正则表达式详解及示例代码

    在编写puppet的pp文件中,会用到很多ruby的正则表达式,常用的正则如下:正则表达式:[codesyntax lang="ruby"]{}: 重复次数(如... [阅读全文]
  • Redis集群搭建全记录

    Redis集群搭建全记录

    redis集群是一个提供在多个redis节点间共享数据的程序集。  redis集群中不支持处理多个keys的命令。  redis集群通过分区来提供一定程度的可用... [阅读全文]
  • Ubuntu上配置Ruby on Rails框架及RubyMine IDE开发环境

    Ubuntu上配置Ruby on Rails框架及RubyMine IDE开发环境

    准备阶段的碎碎念在virtualbox安装过程中由于这样那样的原因,产生许多坑。坑1、关于终端,一定要使用启动器打开安装完虚拟机,进入系统,我们便会遇到第一个坑... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com