HashSets
Learn what hash sets are and how to use them in Ruby.
We'll cover the following...
Listing all keys in a hash
There is a way in Ruby language to list all the keys in a hash. Here is how this method works:
$ pry
> hh = {}
=> {}
> hh[:red] = 'ff0000'
=> "ff0000"
> hh[:green] = '00ff00'
=> "00ff00"
> hh[:blue] = '0000ff'
=> "0000ff"
> hh.keys
=> [:red, :green, :blue]
We defined a hash and pushed a few key-value pairs in it. Keys are a type of symbol, and values are just strings. These strings (ff0000
, 00ff00
, and 0000ff
) are conventional three-byte representations of the color code RGB
, where the first byte is responsible for red (R
) color, the second for green (G
), and the third for the blue (B
).
Getting the list of hash keys isn’t often required. However, there is a need to use keys only in a hash.
Sets
A programmer is free to use hash data structure and arbitrary data for values—like true
, for example—but a ...