Set Default Value In Hash

[Solved] Set Default Value In Hash | Scala - Code Explorer | yomemimo.com
Question : set default value in hash

Answered by : mattias-v

# iterate over default value (0) in the hash frequencies
frequencies = Hash.new(0) #sets default value to 0, so iteration works.
words.each { |word| frequencies[word] += 1 }

Source : https://stackoverflow.com/questions/16159370/ruby-hash-default-value-behavior | Last Update : Fri, 14 Oct 22

Answers related to set default value in hash

Code Explorer Popular Question For Scala