Skip to content
instance method

Hash#shift

() -> [ K, V ]?
shift → [key, value] or nil

Removes and returns the first entry of self as a 2-element array; see Entry Order:

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

Returns nil if self is empty.

Related: see Methods for Deleting.

Ruby version changes

  1. 3.2
    • Hash#shift now always returns nil if the hash is empty, instead of returning the default value or calling the default proc. [Bug #16908]

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