mardi 5 mai 2015

ruby idiom for update or insert a hashmap

Is there a common ruby idiom for this code:

if hashmap.has_key?(key)
    hashmap[key] += 1
else
    hashmap[key] = 1
end

It feels like there might be a high order function that helps here. I'm hoping for something like

hashmap[key].insertOrUpdate { 1 }, {|value| value += 1}

Aucun commentaire:

Enregistrer un commentaire