What I want to do is something like this:
arr = %w(good bad great dull)
"Today was a #{arr.sample} day"
Say I wanted to then loop this 100 times and count each sampled element, and collect the counters in a separate hash or array, how would I do that? And would this be better approached with a hash or an array?
Something like:
counter = []
arr.each { |el| counter << [el => 0] }
100.times do
el = arr.sample
# lost here..
end
Output I want (array or hash):
["good" => 20, "bad" => 20, "great" => 40, "dull" => 20]
I also figure there is a more elegant solution to my first way of thinking.
Perhaps this is what you want?
You could also do:
which is a bit more “self-contained”, and returns the hash itself instead of
100liketimesdoes (though you could also usetapto get that effect).