liveBook · Manning

Hash key

As explained in The Well-Grounded Rubyist, Fourth Edition

An object used to retrieve a corresponding value from a hash.

A hash key identifies the value retrieved from a hash. Hashes store key/value pairs, and both keys and values can be Ruby objects. section 9.3

Looking up a state abbreviation

A state-name string can serve as a key: looking up state_hash[state] retrieves the matching two-letter abbreviation. If the entered state is not defined, the lookup does not produce one of the stored abbreviations. section 9.3

Hash keys support several practical patterns:

  • Use strings, symbols, or other Ruby objects as keys. section 9.3 section 8.2.3
  • Preserve key insertion order, which becomes visible during iteration. section 9.3
  • Use keys for more than direct retrieval, including filtering and selection operations. section 9.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines key/value lookup, demonstrates retrieval and insertion order, but stops short of hash implementation internals.
Examples
Uses state abbreviations, symbol-keyed person data, and Ruby configuration lookups as concrete key examples.
What it assumes
Assumes basic Ruby objects and collections while establishing hashes as collections of key/value pairs.
What you can do after
Apply hash keys to retrieve stored values and access configuration data through bracket lookup.
How it connects
Connects hash keys with hashes, key/value pairs, symbols, strings, arrays, and iteration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.