Ruby pop an element from a hash table? -


i looking @ http://ruby-doc.org/core-1.9.3/hash.html , there not appear pop method? think missing though...

if (x = d['a']) != nil   d.delete('a')  end 

if know key, use delete directly if hash doesn't contain key, nil back, otherwise whatever stored there

from doc linked to:

h = { "a" => 100, "b" => 200 } h.delete("a")                              #=> 100 h.delete("z")                              #=> nil h.delete("z") { |el| "#{el} not found" }   #=> "z not found" 

there shift deletes , returns key-value pair:

hsh = hash.new  hsh['bb'] = 42 hsh['aa'] = 23 hsh['cc'] = 65  p hsh.shift  => ["bb", 42] 

as can seen, order of hash order of insertion, not key or value. doc

hashes enumerate values in order corresponding keys inserted.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -