最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby 中的 module_function 和 extend self異同

 更新時(shí)間:2017年05月23日 08:49:03   作者:hww_面條醬  
本文主要給大家介紹了在Ruby中 module_function 和 extend self的共同點(diǎn)和區(qū)別,非常的詳細(xì),也很實(shí)用,方便大家更好的理解的module_function 和 extend self

在閱讀開(kāi)源的 Ruby 代碼和編寫可維護(hù)性的代碼經(jīng)常遇到這兩者的使用,那么他們兩者的共同點(diǎn)和區(qū)別是什么呢?

module_function

Ruby 的 module 是 method 和 constants 的集合。module 中的method 又可分為 instance method 和 module method, 當(dāng)一個(gè) module 被 include 進(jìn)一個(gè) class ,那么 module 中的 method (注:沒(méi)有被 module_function 標(biāo)記的 method)就是 class 中的 instance method, instance method 需要所在的 class 被實(shí)例化之后才能被調(diào)用;被 module_function 標(biāo)記的 method(不管該 method 是 public 或者 private)就是 module method 且 instance method 也會(huì)變成 private method,對(duì)于被 include 所在的 class 來(lái)說(shuō)是 private method,object.module_name 會(huì)出錯(cuò)。module method 都能被 module_name.method_name 調(diào)用,沒(méi)有被 module_function 標(biāo)記的 public method 不能被 module_name.method_name 調(diào)用。

module 中的 module_function 會(huì)把 module 中的 method 變成 module method 且對(duì)于被 include 所在的 class 來(lái)說(shuō),module method 在 module 中是 private method 故 module_name.module_method 能調(diào)用,而不能被 object.module_name 調(diào)用。

module 中的 public method 對(duì)于被 include 所在的 class 來(lái)說(shuō)是 instance method,故 object.public_method_in_module 能調(diào)用。如果想要非 module method 能夠被 module 調(diào)用(module_name.not_module_method) ,需要引入 extend self (下文會(huì)討論 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

#調(diào)用
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"

總結(jié)就是

•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 本質(zhì)是給 class 或者 module 添加 class method

extend self 讓 module 中的 instance method 能夠被 module_name.instance_method 調(diào)用,保留 module 中原本 method 的 public 或 private 屬性,但又不像 module_function 一樣把被標(biāo)記的 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

# 調(diào)用 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"

總結(jié)就是:
•No method copying involved
•No changes to method visibility

總結(jié)

module_function 改變 module 內(nèi) 原來(lái) method 的 public/private 屬性并把改 method 變成 module method ,能夠被 module_name.module_method 調(diào)用。

extend self 就是在 module 自繼承,不改變 module 中 method 的 public/private 屬性,能夠被 module_name.public_method

相關(guān)文章

  • Ruby常用文件操作代碼實(shí)例

    Ruby常用文件操作代碼實(shí)例

    這篇文章主要介紹了Ruby常用文件操作代碼實(shí)例,如新建文件、輸出文件內(nèi)容、IO操作、輸出文件路徑、stringio使用等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • 使用RVM實(shí)現(xiàn)控制切換Ruby/Rails版本

    使用RVM實(shí)現(xiàn)控制切換Ruby/Rails版本

    RVM 是Ruby Version Manager的縮寫,是一個(gè)命令行工具,它可以讓你輕松地安裝,管理和使用多個(gè)版本的Ruby.不同的rails項(xiàng)目使用等ruby和rails版本不一樣的時(shí)候,可以使用RVM自由切換。
    2017-06-06
  • rudy 重載方法 詳解

    rudy 重載方法 詳解

    rudy 重載方法 詳解...
    2007-11-11
  • Ruby實(shí)現(xiàn)的各種排序算法

    Ruby實(shí)現(xiàn)的各種排序算法

    這篇文章主要介紹了Ruby實(shí)現(xiàn)的各種排序算法,本文給出了Bubble sort、Insertion sort、Selection sort、Shell sort等排序的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-05-05
  • 設(shè)計(jì)模式中的觀察者模式在Ruby編程中的運(yùn)用實(shí)例解析

    設(shè)計(jì)模式中的觀察者模式在Ruby編程中的運(yùn)用實(shí)例解析

    這篇文章主要介紹了設(shè)計(jì)模式中的觀察者模式在Ruby編程中的運(yùn)用實(shí)例解析,觀察者模式中主張?jiān)O(shè)立觀察者對(duì)象來(lái)降低對(duì)象之間的耦合,需要的朋友可以參考下
    2016-04-04
  • 優(yōu)化Ruby腳本效率實(shí)例分享

    優(yōu)化Ruby腳本效率實(shí)例分享

    以前寫過(guò)批量修改繁體文件名為簡(jiǎn)體的Ruby腳本 ,可惜腳本的性能很有問(wèn)題,批量重命名時(shí)運(yùn)行速度非常慢。這次準(zhǔn)備優(yōu)化下代碼,提升腳本的執(zhí)行效率。
    2014-06-06
  • ruby開(kāi)發(fā)vim插件小結(jié)

    ruby開(kāi)發(fā)vim插件小結(jié)

    作為一個(gè)Vimmer和Pythoner,之前折騰過(guò)用python編寫vim插件?,F(xiàn)在作為半個(gè)Rubist,又開(kāi)始繼續(xù)折騰。
    2014-07-07
  • 在Ruby on Rails中優(yōu)化ActiveRecord的方法

    在Ruby on Rails中優(yōu)化ActiveRecord的方法

    這篇文章主要介紹了在Ruby on Rails中優(yōu)化ActiveRecord的方法,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡(jiǎn)單指南

    Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡(jiǎn)單指南

    Rails項(xiàng)目通過(guò)Ruby世界中的gem和rake工具來(lái)構(gòu)建起來(lái)真的相當(dāng)方便,這里就給大家整理了一份Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡(jiǎn)單指南,需要的朋友可以參考下
    2016-06-06
  • Redis集群搭建全記錄

    Redis集群搭建全記錄

    本文給大家總結(jié)了redis集群的概念等基礎(chǔ)知識(shí),以及個(gè)人在搭建redis集群是所遇到的問(wèn)題及解決方法,非常的詳細(xì),有需要的小伙伴可以參考下
    2017-09-09

最新評(píng)論

汤原县| 姜堰市| 教育| 分宜县| 福贡县| 大邑县| 平凉市| 金平| 墨竹工卡县| 建宁县| 田阳县| 内江市| 海安县| 元谋县| 孙吴县| 沾益县| 东港市| 湘潭县| 三明市| 寻甸| 博乐市| 文山县| 西安市| 黎城县| 韶关市| 凤凰县| 天门市| 格尔木市| 克什克腾旗| 弥渡县| 永宁县| 子长县| 武城县| 江都市| 河津市| 鄯善县| 新宁县| 丘北县| 界首市| 克拉玛依市| 澄迈县|