我正在创建一个类,然后尝试在其中执行一些操作,但由于某种原因,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'。 –