我正在从Poignant Guide to Ruby中学习Ruby,在一些代码示例中,我遇到了双冒号和点的用法,它们似乎用于相同的目的:
File::open( 'idea-' + idea_name + '.txt', 'w' ) do |f|
f << idea
end
在上面的代码中,双冒号用于访问File
类的open
方法.然而,我后来遇到了出于同样目的使用点的代码:
require 'wordlist'
# Print each idea out with the words fixed
Dir['idea-*.txt'].each do |file_name|
idea = File.read( file_name )
code_words.each do |real, code|
idea.gsub!( code, real )
end
puts idea
end
这一次,一个点被用来访问File
类的read
方法.以下两者之间的区别是什么:
File.read()
和
File::open()