My hashe is only storing 1 set of values

Dir.foreach(directory) do |f|
@file_sizes = {f => File.size(directory+"/"+f)}
end

So I’m storing the file name and size in a hash, with the name of the
file as the key.

The above piece of code seems to only store the last file in the
directory, why is that?

I’m building a program that will get all the files in a directory, grab
their file sizes. Then I’m gonna to output the 10 biggest files.

My design is:
put each filename and size into a hash, reverse the key and value (I
know I can get rid of this step), then output the first 10 sorted array
values. Is this a good way to do this or is there a better way?

OMG, sorry i just figured out what I did wrong.

delete thread

Quoth Feng T.:

Dir.foreach(directory) do |f|
@file_sizes = {f => File.size(directory+"/"+f)}
end

Should be:

@file_sizes ||= {}
Dir.foreach(directory) do |f|
@file_sizes[f] = File.size(directory + “/” + f)
end

HTH,