Do as below :
def score( array )
hash = Hash.new(0)
array.each{|key| hash[key] += 1}
hash
end
score([1,2,4,5,4,7]) # => {1=>1, 2=>1, 4=>2, 5=>1, 7=>1}
Or more Rubyish using Enumerable#each_with_object:
def score( array )
array.each_with_object(Hash.new(0)){|key,hash| hash[key] += 1}
end
score([1,2,4,5,4,7]) # => {1=>1, 2=>1, 4=>2, 5=>1, 7=>1}
The reason of why NoMethodError: undefined method '+' for nil:NilClass ?
hash = {} is an empty has,with default value as nil.nil is an instance of Nilclass,and NilClass doesn't have any instance method called #+. So you got NoMethodError.
Look at the Hash::new documentation :
new → new_hash
new(obj) → new_hash
Returns a new, empty hash. If this hash is subsequently accessed by a key that doesn’t correspond to a hash entry, the value returned depends on the style of new used to create the hash. In the first form, the access returns nil. If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.
hash = {}tohash = Hash.new(0). That tells Ruby that if it encountershash[key]in a context where it must have a value (such ashash[key] += 1orv = hash[key]) and the hash does not contain the keykey, it is to addkey=>0to the hash before taking further action (such ashash[key] += 1). On the other hand,if hash[key] == 7will evaluate toif nil == 7ifkeyis not in the hash;key=>0will not be added to the hash.