我正在存储Hash of Hash。
filelists { filename => [another hash] }
anotherhash {id => method}
其中filename是文件名,id是服务器id,method是锁,读或写的类型。
def addfile(client, input)
#byebug
serverid = input.split(' ')[2].strip
filename = input.split(' ')[1].strip
puts "RECEIVED FILE NOTIFICATION: " + serverid + " FILENAME: " + filename
if @filelists.include?(filename)
@filelists[filename] = {serverid.to_i => 'r'}
else
@filelists[filename] = Hash.new
@filelists[filename] = {serverid.to_i => 'w'}
puts "New Hash Created for: " + filename
end
client.puts "File Accepted: " + filename
puts "File Accepted: " + filename + "\n\n"
printfiles(filename)
end
当向哈希中添加另一个元素时,它只会删除前一个元素。
第一台服务器上传文件后,有:
{"ds.txt"=>{1=>"w"}, "fileserver.rb"=>{1=>"w"}}
然后,当第二台服务器开始上传文件时,它只会重写上一个哈希值。
任何意见/建议都将不胜感激。
答案 0 :(得分:0)
使用了错误的格式。需要改为
@filelists[filename][serverid.to_i] = 'r'
最终代码
def addfile(client, input)
#byebug
serverid = input.split(' ')[2].strip
filename = input.split(' ')[1].strip
puts "RECEIVED FILE NOTIFICATION: " + serverid + " FILENAME: " + filename
if @filelists.include?(filename)
@filelists[filename] = {serverid.to_i => 'r'}
else
**@filelists[filename][serverid.to_i] = 'r'**
puts "New Hash Created for: " + filename
end
client.puts "File Accepted: " + filename
puts "File Accepted: " + filename + "\n\n"
printfiles(filename)
end