Я изучаю Ruby из Poignant Guide to Ruby, и в некоторых примерах кода я сталкивался с использованием двойного двоеточия и точки, которые, кажется, используются для той же цели:
File::open( 'idea-' + idea_name + '.txt', 'w' ) do |f|
f << idea
end
В приведенном выше коде двойное двоеточие используется для доступа к методу open
класса File
. Однако позже я наткнулся на код, который использовал точку для той же цели:
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
На этот раз точка используется для доступа к методу read
класса File
. В чем разница между:
File.read()
а также
File::open()