Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

How to sum repetitions of a value and add it in two values of a key in Ruby?

$
0
0

Im trying to to create a hash with one key per each type of extension on a directory. To every key I would like to add two values: number of times that extension is repeated and total size of all the files with that extension.

Something similar to this:

{".md" => {"ext_reps" => 6, "ext_size_sum" => 2350}, ".txt" => {"ext_reps" => 3, "ext_size_sum" => 1300}}

But I´m stuck on this step:

hash = Hash.new{|hsh,key| hsh[key] = {}}ext_reps = 0ext_size_sum = 0Dir.glob("/home/computer/Desktop/**/*.*").each do |file|  hash[File.extname(file)].store "ext_reps", ext_reps  hash[File.extname(file)].store "ext_size_sum", ext_size_sum endp hash

With this result:

{".md" => {"ext_reps" => 0, "ext_size_sum" => 0}, ".txt" => {"ext_reps" => 0, "ext_size_sum" => 0}}

And I can't finde the way to increment ext_reps and ext_siz_sum

Thanks


Viewing all articles
Browse latest Browse all 4615

Trending Articles