You may enjoy Hash constructors and what you can do with them.
For primitive values you can use Hash.new(0) to create something that works like a counter.
For more complex types, you need the block format, Hash.new { |h, k| h[k] = {} }. This ensures a brand new object for each key that the hash doesn't know about as a default value, rather than reusing the same one repeatedly. This also works with hashes containing arrays. You could even go as far as to mix the two and get something like this:
This works because h.default_proc is referring to the function that is called when a default value is needed, or in other words, magic recursive hashes:
Now names. Names names names, names are very important. color_gender_lives relies on knowledge of the data structure. What if it changed? Color, gender, and lives are all properties, or perhaps attributes of a pigeon. Perhaps attribute_name or property?
That would bring us to value and stats and all_names. They're not as much values as they are the actual attributes or properties of a pigeon.
Combining all these ideas, we might get code that looks something close to this:
You may enjoy
Hash
constructors and what you can do with them.For primitive values you can use
Hash.new(0)
to create something that works like a counter.For more complex types, you need the block format,
Hash.new { |h, k| h[k] = {} }
. This ensures a brand new object for each key that the hash doesn't know about as a default value, rather than reusing the same one repeatedly. This also works with hashes containing arrays. You could even go as far as to mix the two and get something like this:...and the most fun trick? You could make an infinitely deep hash if you really wanted to:
This works because
h.default_proc
is referring to the function that is called when a default value is needed, or in other words, magic recursive hashes:Using those ideas you can avoid the
nil
checks. If you were to use||=
instead it would have looked like this:...which would take out four extra lines.
Now names. Names names names, names are very important.
color_gender_lives
relies on knowledge of the data structure. What if it changed? Color, gender, and lives are all properties, or perhaps attributes of a pigeon. Perhapsattribute_name
orproperty
?That would bring us to
value
andstats
andall_names
. They're not as much values as they are the actualattributes
orproperties
of a pigeon.Combining all these ideas, we might get code that looks something close to this:
Thanks Brandon, that was a super useful explanation and insight,....just awesome stuff