Skip to content

Instantly share code, notes, and snippets.

@JoelLindow
Last active May 2, 2017 14:44
Show Gist options
  • Save JoelLindow/e3715955a6a8bd9380f76ee17631e631 to your computer and use it in GitHub Desktop.
Save JoelLindow/e3715955a6a8bd9380f76ee17631e631 to your computer and use it in GitHub Desktop.
Ruby Hashes for Dummies - By Joel Lindow

Hashes

Hashes are Heterogenous, meaning it can hold a mixture of data types.

Here are a few examples of things you can do to use hashes in Ruby.

Let's start with a basic hash:

hash = { Jackson: 17, Sarah: 19, Johnson: 25, Mike: 75 }

Now let's play with that hash! Pay attention to the return values!

> hash.values 			
=> [17, 19, 25, 75]

> hash.keys 			
=> [:Jackson, :Sarah, :Johnson, :Mike]

> hash[:Joel] 			
=> nil

We just returned nil, because :Joel is not in the hash and can not return a value. Let's keep playing!

> hash.merge({ :Joel => 36 }) 	
=>  {:Jackson=>17, :Sarah=>19, :Johnson=>25, :Mike=>75, :Joel=>36}

There! We added a new "key value pair". But wait, we don't want it in ther anymore. Let's delete it!

> hash.delete(:Joel)
=> nil

> hash
=> {:Jackson=>17, :Sarah=>19, :Johnson=>25, :Mike=>75}

Wait! I actually DO want Joel in this hash! Let's add it again a different way!

> hash[:Joel]=36
=> 36

> hash
=> {:Jackson=>17, :Sarah=>19, :Johnson=>25, :Mike=>75, :Joel=>36}

Here are some examples of how to iterate through keys and values to return things you might want out of the hash. Keep in mind that you're still returning the original value of the "hash" hash, as you didn't really do anything with the values you iterated through.

> names = hash.each_key { |k| puts k } < iteration through all keys

> values = hash.each_value { |x| puts x } < iteration through all values

Now, here's an example of actually returning the value you wanted using iteration, and creating a new variable that holds that value (or at least holds the enumerator and formula to get the return you want out of the hash. Keep in mind that if somebody added another name to this hash that has a value greater than 21, it would start to show up when you call the "old_enough" object, as it is not just a value... but it is a return value. Why? Because... "old_enough" is an enumerator, just like you set it to be in the formula below!

> old_enough = hash.select { |k,v| v > 21 } < iteration looking for conditional on v
=> {:Johnson=>25, :Mike=>75, :Joel=>36}

> old_enough
=> {:Johnson=>25, :Mike=>75, :Joel=>36}

Here's another example using another hash called "menu"

Let's start with a fresh hash! Let's create a menu for a fast food restaurant.

> menu = {}

It would be a good idea to set up the basic types of sandwiches we want to serve. So, Let's create a Sandwiches key and within that key set our value to another hash full of sandwich types with empty arrays for their value. We will add menu items to these different sandwich types as we build the menu.

> menu[:Sandwiches] = {:Beef => [], :Chicken => [], :Pork => [], :Breakfast => []}
=> {:Beef=>[], :Chicken=>[], :Pork=>[], :Breakfast=>[]}

OH CRUD! We already knew that we wanted to add the different sandwiches our restaurant serves to our menu! But we set the different sandwich types values to empty arrays. However, we need to add sandwiches and their prices... which means we actually need to add more "key value pairs". So... we don't need empty arrays. We need empty hashes that we can add sandwiches and prices to as we build our menu. Better fix that!

> menu[:Sandwiches].each do |k,v|
>    menu[:Sandwiches][k] = {}
> end
=> {:Beef=>{}, :Chicken=>{}, :Pork=>{}, :Breakfast=>{}}

Much better! Now, let's start with the Beef sandwiches. We can add our hamburger menu items into the hash where they beling, and also assign them a price.

> menu[:Sandwiches][:Beef].merge!({:Cheese_Burger => 1.00, :Double_Cheese_Burger => 1.50, :Triple_Cheese_Burger => 2.00})

=> {:Cheese_Burger=>1.0, :Double_Cheese_Burger=>1.5, :Triple_Cheese_Burger=>2.0}

Return value of what we added looks right... but did it actually add to our menu? Let's call on the menu variable and see what's going on in the stored hash!

> menu
=> {:Sandwiches=>{:Beef=>{:Cheese_Burger=>1.0, :Double_Cheese_Burger=>1.5, :Triple_Cheese_Burger=>2.0}, :Chicken=>{}, :Pork=>{}, :Breakfast=>{}}}

Yep! Looks Good so far! Let's add some more menu items!

> menu[:Sandwiches][:Chicken].merge!({:Basic_Chicken_Sandwich => 2.00, :Chicken_Club => 3.50, :The_Big_Clucker => 6.50})
=> {:Basic_Chicken_Sandwich=>2.0, :Chicken_Club=>3.5, :The_Big_Clucker=>6.5}

> menu[:Sandwiches][:Breakfast].merge!({:Egg_Biscuit => 1.50, :Sausage_And_Egg_Biscuit => 3.00, :Grandpas_Four_Meat_Breakfast_Sammy => 6.00})
=> {:Egg_Biscuit=>1.5, :Sausage_And_Egg_Biscuit=>3.0, :Grandpas_Four_Meat_Breakfast_Sammy=>6.0}

> menu
=> {:Sandwiches=> {
      :Beef=>{:Cheese_Burger=>1.0, :Double_Cheese_Burger=>1.5, :Triple_Cheese_Burger=>2.0},
      :Chicken=>{:Basic_Chicken_Sandwich=>2.0, :Chicken_Club=>3.5, :The_Big_Clucker=>6.5},
      :Pork=>{},
      :Breakfast=>{:Egg_Biscuit=>1.5, :Sausage_And_Egg_Biscuit=>3.0, :Grandpas_Four_Meat_Breakfast_Sammy=>6.0}}
      }

CONTINUE WRITING HERE...............

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment