Skip to content
Ruby

Hash API Reference

Ruby's Hash — a dictionary mapping unique keys to values, preserving insertion order.

By EZ4Code Team

Hash

A Hash maps each of its unique keys to a specific value, preserving insertion order since Ruby 1.9.

Hash#store(key, value) -> value

Associates the given value with the given key. Equivalent to h[key] = value.

Returns: Object

Hash#fetch(key, default=nil) -> value

Returns the value for the given key. If key not found, returns default or raises KeyError.

Returns: Object

Hash#keys -> Array

Returns a new array populated with the keys from this hash.

Returns: Array

Hash#values -> Array

Returns a new array populated with the values from this hash.

Returns: Array

Hash#each {|key, value| block} -> Hash

Calls the block once for each key/value pair, passing key and value as parameters.

Returns: Hash

Hash#size -> Integer

Returns the number of key-value pairs. Alias: length.

Returns: Integer

Hash#delete(key) -> value

Deletes the key-value pair and returns the value. Returns nil if key not found (or yields block).

Returns: Object

Hash#has_key?(key) -> bool

Returns true if the given key is present. Aliases: key?, member?, include?.

Returns: Boolean

More Ruby API References