Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Hash#shift

hash.shift → [key, value] or default_value

Removes the first hash entry (see Entry Order); returns a 2-element Array containing the removed key and value:

Example
h = {foo: 0, bar: 1, baz: 2}
h.shift # => [:foo, 0]
h # => {:bar=>1, :baz=>2}

Returns the default value if the hash is empty (see Default Values).

See also #select! #size #select #slice also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1