Arrays and Hashes

If you were writing a cookbook

April 4, 2015

place kitten

What is an array

An array is kind of like a bucket that allows you to put anyting in it. You could put strings(words) like "avocado" and "tomatoes." You could also put numbers in your bucket like "2" or "300." You could also put a smaller bucket of things that might already have "cilantro" and "lime" in it.

How to create an array

1. The bucket must have a name

salsa_ingredients

2. You will tell the bucket what you want in it by using the equal sign = and square brackets [ ] which will act as your bucket

salsa_ingredients = ["tomato"]

3. The items in your bucket have to be surrounded by quotation marks and separated by commas

salsa_ingredients = ["tomato", "onions", ["salt", "pepper", "lime"], "cilantro"]

Notice that I put a smaller bucket of flavor ingredients that include salt, pepper and lime. The smaller bucket should also follow the rules of being inside square brackets, surrounded by quotes and separated by commas

How to access items in an array

The cool thing about arrays is that each item that is thrown into the bucket is given a number, starting at 0. These numbers are called "indexes," so the "tomato" is at index 0, "onions" are index 1, the smaller bucket of flavors are index 3, and so on.

In order to access the item at index 3, for example, you would type:

salsa_ingredients[3]

and you would get

"cilantro"

What would I use an array for?

Arrays allow you to take the items in the bucket and do things with it. For exmple, in ruby code, I could print out a list of all the items in the array.

salsa_ingredients.each do |ingredient|
    puts ingredient
end


and you would get the following output:

tomato
onions
salt
pepper
lime
cilantro

You might think, "well, I could have just written that out!" Well, suppose you were planning to create a cookbook that included ingredients for fish tacos or home-made guacamole and at least 30 other recipes. You would be able to access all the ingredients inside of the bucket by simply knowing the name of the bucket. It's good to have lists of things organized in buckets. :)

What is a hash

A hash is similar to an array, it's a bucket, but instead of keeping track of things with index numbers, you get to keep track of things by giving it names, or keys, as referred to in ruby.

How to create a hash

1. The bucket must have a name

todays_special

2. The bucket will be contained in curly braces this time {}

todays_special = {}

3. Remember how arrays had indexes that started from 0? Well, instead of 0, 1, 2, 3, you can actually name your index(it's now called "keys") by using square brackets and quotes [""], then telling it what the items are. So:

todays_special["appetizer"] = "chips and salsa"
todays_special["main"] = "pork tamales"
todays_special["dessert"] = "churros"


So in the end you'd end up with something that looks like this:

todays_special = {
    todays_special["appetizer"] = "chips and salsa"
    todays_special["main"] = "pork tamales"
    todays_special["dessert"] = "churros"
}


How to access items in a hash

It's pretty simple, kind of like how you could access items in an array by saying "gimme the item at index[3]," you could tell a hash to give you the item at they key value "dessert"

todays_special["dessert"]

would return:

"churros"

What would I use a hash for?

Is that confusing? Well, like I said, hashes are like arrays, they're bukets of things, but it's much more flexible in that you aren't restricted to keeping track of the things in your bucket using numbers.

So in an array, you might have forgotten what index the main course was. With a hash, you can just say, "give me the value that has the key named 'main'"

Conclusion

This little blog entry doesn't cover everything. There are other ways to create hashes, using syntax like "hash rockets" or symbols, and also powerful methods that allow you to manipulate hashes and create complex data structures. Either way, hopefully now you know a little bit about how you can categorize and bucket data in programming, using ruby!



Previous Next