Module extend self

栏目: IT技术 · 发布时间: 5年前

内容简介:Buried in this blog post, I found an interesting construction. See the ExampleI had never seen theUsing

module-extends-self

Module extend self

Buried in this blog post, I found an interesting construction. See the Example module OrderRepository .

I had never seen the extend self construct before, so I did a little playing around in Pry.

Using extend self in a module is similar to calling class << self ... end with all the method definitions inside it. It’s yet another way to get access to a module’s eigenclass.

This goes along with using def self.my_method and using module_function to also making piecemeal module methods, the main distinction being that using def self.my_method creates a method that the only receiver will be the module itself, not any classes that include the module.

module MyMod
  extend self

  def hi_there
    "hi there from inside MyMod"
  end

  private

  def go_away
    "go away now inside MyMod"
  end
end

MyMod.hi_there #=> "hi there from inside MyMod"
MyMod.go_away

class MyClass
  include MyMod

  def call_hi_there
    hi_there
  end
end

MyClass.hi_there
MyClass.new.hi_there #=> "hi there from inside MyMod"
MyClass.call_hi_there
MyClass.new.call_hi_there #=> "hi there from inside MyMod"
MyClass.go_away
MyClass.new.go_away

module YourMod
  class << self
    def hi_there
      "hi there from inside YourMod"
    end

    private

    def go_away
      "go away now inside YourMod"
    end
  end

  def que?
    "que? inside YourMod"
  end
end

YourMod.hi_there #=> "hi there from inside YourMod"
YourMod.go_away
YourMod.que?

module TheirMod
  def hi_there
    "hi there from inside TheirMod"
  end

  method_function :hi_there
end

TheirMod.hi_there #=> "hi there from inside TheirMod"

module ItsMod
  def self.hi_there
    "hi there from inside ItsMod"
  end
end

ItsMod.hi_there #=> "hi there from inside ItsMod"


class YourClass
  include YourMod

  def call_hi_there
    hi_there
  end
end

YourClass.new.hi_there #=> NoMethodError: undefined method `hi_there' for #<YourClass:0x007f50d0307cd0>
YourClass.new.call_hi_there #=> NameError: undefined local variable or method `hi_there' for #<YourClass:0x007f50d021aae8>

class TheirClass
  include TheirMod

  def call_hi_there
    hi_there
  end
end

TheirClass.new.hi_there #=> NoMethodError: private method `hi_there' called for #<TheirClass:0x007f50d0978880>
TheirClass.new.call_hi_there #=> "hi there from inside TheirMod"

class ItsClass
  include ItsMod

  def call_hi_there
    hi_there # not available!
  end

  def really_call_hi_there
    ItsMod.hi_there
  end
end

ItsClass.new.hi_there #=> NoMethodError: undefined method `hi_there' for #<ItsClass:0x007f50d0859aa8>
ItsClass.new.call_hi_there #=> NameError: undefined local variable or method `hi_there' for #<ItsClass:0x007f50d0817450>
ItsClass.new.really_call_hi_there #=> "hi there from inside ItsMod"

以上所述就是小编给大家介绍的《Module extend self》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

第四次革命

第四次革命

[意]卢西亚诺•弗洛里迪(Luciano Floridi)著 / 王文革 / 浙江人民出版社 / 2016-5 / 64.90元

 随着线上线下大融合以及人工智能的极大发展,人类已经进入超历史时代。在这一时代中,人类终于迎来了继哥白尼革命、达尔文革命、神经科学革命之后自我认知的第四次革命——图灵革命,整个世界正化身为一个信息圈,每个人都生活在云端,人类已不再是信息圈毋庸置疑的主宰。毫无疑问,图灵革命引爆了人工智能重塑整个人类社会的序曲!  那么在人工智能时代,人类如何保证自己最钟爱的财富——“隐私”不被窃取?如何应......一起来看看 《第四次革命》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具