ruby 来自http://stackoverflow.com/questions/10039039/why-self-method-of-module-cannot-become-a-singlet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 来自http://stackoverflow.com/questions/10039039/why-self-method-of-module-cannot-become-a-singlet相关的知识,希望对你有一定的参考价值。

module YourModule
  module ClassMethods
    def a_class_method
      puts "I'm a class method"
    end
  end

  def an_instance_method
    puts "I'm an instance method"
  end

  def self.included(base)
    base.extend ClassMethods
  end
end

class Whatever
  include YourModule
end

Whatever.a_class_method
# => I'm a class method

Whatever.new.an_instance_method
# => I'm an instance method

以上是关于ruby 来自http://stackoverflow.com/questions/10039039/why-self-method-of-module-cannot-become-a-singlet的主要内容,如果未能解决你的问题,请参考以下文章