我正在創建一個類,然後嘗試在其中執行一些操作,但由於某種原因,Ruby無法查看我在頂部定義的類變量,因此有任何想法?未定義變量錯誤
class GoogleChart
BASE_URL = "http://chart.apis.google.com/chart"
URL_MAX_LENGTH = 2074 #found this online
help_file = "help_attr_hash.txt"
attr_file = "attr_hash.txt"
attr_hash = nil
help_hash = nil
def parseHashFromFile(filename)
return if filename == nil
hash_array = []
#open the file
f = File.open(filename)
#read each line and strip off '\r\n'
f.each do |line|
hash_array.push(line.chomp!)
end
#convert to a Hash {x => y}
hash = Hash[*hash_array.flatten]
return hash
end
def start
attr_hash = parseHashFromFile(attr_file) ##Cant see attr_file
help_hash = parseHashFromFile(help_file) ##Cant see help_file
puts attr_hash
puts help_hash
end
if __FILE__ == $0
start()
end
end
感謝
啊我看到謝謝你的評論,你介意|| =做什麼?我只見過||作爲邏輯或。 –
@Hunter:'a || = b'是'a = a ||的簡寫。 b',所以只有當'a'還沒有真值時,它纔是一個簡單的方法來將'b'賦值給'a'。如果a.nil?'較長且較少慣用的版本將是'a = b'。 –