dbc_hashes = {Hashes => "Awesome"}

dbc_arrays = ["Amazing"]

16/11/2014

When you have lots of similar content that you wanna store on variables, you could create multiple variables, but that wouldn't really be an effective solution, would it?

That's why we have arrays! Instead of my_book1, my_book2, my_book3, etc. You could just create an array called my_books and store all the books in there. Just like this:

my_books = ["Harry Potter and The Philosopher's Stone", "Harry Potter and The Half-Blood Prince", "Harry Potter and The Deathly Hallows"]

Now, isn't that better? But what if you wanted to store your books' titles and say... their ratings on the same place, can you do that? You can! That's why we have Hashes!

Hashes works with a key => value system. Just like this:

my_books_ratings = { "Harry Potter and The Philosopher's Stone" => "Can this get any better?", "Harry Potter and The Half-Blood Prince" => "Maybe it can!", "Harry Potter and The Deathly Hallows" => "Great stuff!" }

You can access the content on arrays using array[index], for example my_books[0] would return "Harry Potter and The Philosopher's Stone". You can also access everything on the array using iteration. You can do:

my_books.each do |x| puts x end

The code above places the books on the placeholder x and you can do whatever you want with it, on this case we printed all the names.

As for accessing the content on hashes, you can just type the name of your hash and you will get the names and keys inside it. If you just want the value to a specific key you can do my_books_ratings["Harry Potter and The Philosopher's Stone"] and you will get "Can this get any better?" as its value. You can iterate access everything on the hash using iteration. You can do:

my_books_ratings.each do |x, y| puts "Title: #{x}, Rating: #{y}" end

The code above the titles on the placeholder x and the ratings on the placeholder y and you can do whatever you want with them, on this case we printed them side by side. Be mindful that you can access only the values, only the keys or both of them using the methods .keys and .values after calling the hash.