Listing 8.7: mods.rb为什么在控制台输入后无结果?
module Module_Adef print_hello
puts "Hello!"
end
end
module Module_B
def print_hello
puts "Hi there!"
end
end
把以上代码输入后,Module_A.print_hello与 Module_B.print_hello均报错。
经各调试后,发现把代码改为下面后,就可行了:
module Module_A
def self.print_hello
puts "Hello!"
end
end
module Module_B
def self.print_hello
puts "Hi there!"
end
end
个人的理解是,module与class有着相似的属性,所以在声明方法的时候也需要采用与class一样的方式。这是楼主的个人见解,还请大家予以指正。
页:
[1]