最新国产好看的视频,伊人天堂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

在閱讀開源的 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 (注:沒有被 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,對于被 include 所在的 class 來說是 private method,object.module_name 會(huì)出錯(cuò)。module method 都能被 module_name.method_name 調(diào)用,沒有被 module_function 標(biāo)記的 public method 不能被 module_name.method_name 調(diào)用。

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

module 中的 public method 對于被 include 所在的 class 來說是 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) 原來 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è)立觀察者對象來降低對象之間的耦合,需要的朋友可以參考下
    2016-04-04
  • 優(yōu)化Ruby腳本效率實(shí)例分享

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

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

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

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

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

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

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

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

    Redis集群搭建全記錄

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

最新評論

黄浦区| 三门峡市| 乌兰县| 聂拉木县| 德安县| 襄樊市| 屏东县| 逊克县| 闻喜县| 东丰县| 甘南县| 平遥县| 离岛区| 久治县| 安顺市| 余干县| 章丘市| 高雄市| 道孚县| 津市市| 东台市| 东光县| 元朗区| 浦东新区| 莫力| 乌什县| 万安县| 昌都县| 齐河县| 石景山区| 林芝县| 连城县| 洱源县| 漯河市| 剑川县| 平山县| 宁津县| 三穗县| 翁牛特旗| 广汉市| 梨树县|