我從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()
參見[什麼是Ruby的雙冒號(::)所有關於(http://stackoverflow.com/questions/3009477/what-is-rubys-雙冒號 - 全約)。 – sczizzo
我真的不認爲這是重複的。在這個問題或其答案中沒有任何地方使用關於所討論的單例方法的'::'。 –
我同意。在我打開這個之前,我看到了另一個問題。但是,我的問題的答案沒有明確給出。另一個問題討論僅使用::運算符。也許它暗示了以微妙的方式使用點運算符,但是當你對某些東西不熟悉時,你需要以非常不同的形式回答。就像我說的,我對ruby很陌生。 – flyingarmadillo