In today's blog I'll be discussing the differences between Hashes and Arrays. Let's start by looking at an array.
jedi_counsel = ["Obi Wan", "Yoda", "Mace Windu", "Plo Koon"]
Above, we have an array. Basically, we've just created a list of items, in this case some seriously badass peeps. This list has been given the title jedi_counsel. In any array, each listed item is awarded a number. In this case Obi Wan is number 0, Yoda is number 1, and so on. This is the order of numbering in arrays. This is also one of the only ways to access individual items in an array. Okay great we can make lists! But, wouldn't it be kind of great if we had another, even more advanced list, where we could assign different values to list items. Kinda like a restaurant menu? Yeah...that would be great.
menu = Hash.new
menu = {
:cheeseburger => 5
:hamburger => 4
:salad => 10
}
Other then healthfoods being outrageously priced these days you may also notice that in this list each item has a partner. This is called a HASH. In a hash, you can assign values to what are called "keys" for easy access and arrangement. Hashes can also be assigned a default value so if a key is accessed that has not been assigned a value the default will be returned.
I hope this blog post helps you in your learning!
Thanks for reading,
-Ricky