Skip to content

Instantly share code, notes, and snippets.

@goodviber
Last active February 17, 2020 14:54
Show Gist options
  • Select an option

  • Save goodviber/96e4017cd51390e9e99f8b68a2f04271 to your computer and use it in GitHub Desktop.

Select an option

Save goodviber/96e4017cd51390e9e99f8b68a2f04271 to your computer and use it in GitHub Desktop.
Hash values as arrays
@avg_score_hash = Hash.new { |hash, key| hash[key] = [] }
@rows.each do |row|
row.last.each_with_index do |column, index|
if column != 0
@avg_score_hash[index] << column
end
end
end
@avg_score_hash
#=> {2=>[92.73], 5=>[90.91, 95.56, 90.91], 3=>[93.33], 0=>[100.0]}
@avg_score_hash.map { |k,v| [k, [v.sum/v.size]]}.to_h
#=> {2=>[92.73], 5=>[92.46], 3=>[93.33], 0=>[100.0]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment