Module.fun和fun之间有什么区别?

时间:2013-03-27 05:56:25

标签: ruby

module MyMod
  def fun1
  #...
  end 

  def MyMod.fun2
  #...
  end
end

fun1和MyMod.fun2有什么区别?

1 个答案:

答案 0 :(得分:4)

fun1是一种实例方法。只有当任何类在其定义中包含该模块时才能访问它。

p RUBY_VERSION

module MyMod

  def fun1
  p "hi"
  end 

  def MyMod.fun2
  p "hello"
  end
end

class Foo
include MyMod
end
Foo.new.fun1
MyMod.fun2
p MyMod.instance_methods(false)
p MyMod.public_class_method("fun2")

输出:

"2.0.0"
"hi"
"hello"
[:fun1]
MyMod
相关问题