- 定义self.method,在之后就可以直接用类名来调用方法了,类似Java的static方法。
- include的先后顺序是有意义的,『这里不同于Java的import,Java的import顺序无关』先include的module会在ancestor树的上方,更靠近BasicObject。
module My_a
def self.dosth
puts "This is A do sth."
end
end
module My_b
def dosth
puts "This is B do sth."
end
end
class My
include My_a
include My_b
def doit
puts "明确调用My_a的dosth方法.\n"
dosth
My_a.dosth
end
end
puts My.ancestors.to_s
# =>
# [My, My_b, My_a, Object, Kernel, BasicObject]
obj0 = My.new
obj0.doit
# =>
# 明确调用My_a的dosth方法.
# This is B do sth.
# This is A do sth.