1. Computing

Everything You Wanted To Know About Hashes


Everything You Wanted To Know About Hashes

Hashes are the bread and butter of Ruby data structures. For any unordered structure, you'll find hashes used everywhere. If it's ordered, you'll see arrays used, and occasionally you'll see small data structures be formed with objects and instance variables, or rarely with Struct and OpenStruct objects. But for everything else, it's hashes? Why? It probably has to do with how easy Hashes are to use, how much typing it saves, and to some extent, programmer laziness. Hashes are not the fastest things out there, but they're the handiest. And trading CPU time for programmer time is usually a good idea in the eyes of a programmer, at least until their benchmarks tell them otherwise.

The following is a series of links to several useful articles on Hashes, ordered roughly from most basic to most advanced.

  • Hashes - This is the most basic article here, an introduction to hashes. A formal explanation of what hashes are (key/value storage), hash literals and how to use hashes in everyday code.
  • Building Hashes - Building Hashes digs a little deeper, showing how to build hashes from literals, building hashes from serialized data like YAML or JSON (technically not a "hash" topic, but it's something you'll run into quite often), building hashes by merging other hashes and building hashes from arrays.
  • Creating Hashes From Arrays - This article looks more deeply into the technique of creating hashes from Arrays. It's not a terribly common thing to do, but it does involve many aspects of Ruby. The Array must be formed, often collated with column names, massaged into the correct format, flattened, splatted and finally fed into a Hash constructor. It's something you can do in your sleep once you understand all these concepts, but it's certainly a dance to get it right.
  • Optional Method Parameters with Hashes - Ruby does not have optional method parameters. These, sometimes called keyword parameters, allow users of an API to only pass the essential parameters of a method while ignoring the non-essential parameters, passing them out of order and by name when necessary. Ruby doesn't have this feature, nor does it have the ability to call different methods depending on the number of parameters (as with C++), but this behavior can be emulated by using Hashes. And Ruby helps you along with some easy syntax, if you didn't know you'd swear it really was a language feature, but it's really just a hash hack.
  • Advanced Hash Usage - While most every time you use a hash, it will be as a simple key/value store, there are times when you're going to have to dig deeper. What if you retrieve a value when nothing has been stored under that key. Do you want to return nil (the default behavior), another value, or raise an exception? In this article you will also see various tricks mentioned in previous articles including keyword parameters, and you'll also see an alternative and new (to 1.9.x, at least, which has been around for a few years already) hash syntax that makes hashes with strictly symbol keys have a cleaner syntax.
  • Hash Tips and Tricks - This is another collection of shorter pieces, much like Advanced Hash Usage. In this article you'll learn how to index anything, not just the key/value store, using hashes. You'll learn how to store the return values of a method to use a hash as an automatic method cache, speeding up expensive methods that (should) return the same thing every time it's called. You'll learn how Hashes power OpenStruct, and how you can emulate the same dot syntax in your own code. You'll learn the power of "hashes of hashes" to make arbitrary tree structures. You'll learn how to make "strict" hashes, that raise exceptions when an invalid key is accessed. You'll even learn how hashes and format strings interact, making printing values from hashes a much simpler proposition, cleaning up your output string considerably.
  • Hash Reference - And finally, this article is a reference of the most common or more useful hash methods. While the official documentation holds much more information, it also tells you a whole lot about methods you probably don't care about. Since the Hash class is used so often, it's accumulated quite a lot of methods you'll probably never use, and are only useful in very specific cases. This reference tries to narrow the focus down, and to explain a bit more deeply the things you probably really want to know about. And, if you need to step outside the norm to one of the more esoteric methods, there's a link to the official Ruby documentation on Hashes, should you need it.
  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Ruby's Basic Features
  6. Everything You Wanted To Know About Hashes

©2014 About.com. All rights reserved.