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?

···

--
Posted via http://www.ruby-forum.com/.

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

delete thread

···

--
Posted via http://www.ruby-forum.com/.

Quoth Feng Tien:

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,

···

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/