Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Hashes Keys and Values

When do you use an array versus a hash in Ruby?

In the Hashes section of the Ruby course, the instructor, Jason Seifer, says that hashes are "similar" to arrays. So far in the lesson, the two techniques do seem to provide a lot of similar functionality. Although the instructor mentions they are similar, he doesn't say when a programmer might use a hash instead of an array, or vise versa. I'm trying to relate to how this might work in a real world situation.

I'm just getting started with Ruby, so I may be misunderstanding or missing important differences between arrays and hashes. Can someone explain key differences/similaries between hashes and arrays, and when a Ruby programmer might be preferred (or require) one over the other. I'm trying to translate the abstract concepts to the real world. Thanks for helping!

Jamie Barton
Jamie Barton
14,498 Points

I'd use an Array to contain a bunch of Hashes.

Think of an Array containing a list of Users. Each User may contain a Hash of their details like firstName, lastname, Dob, etc..

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

From my perspective that shows similarity and main difference:

Array simply contains some values and they can be accessed through IMPLIED keys that are numbers from 0 up. So if you have this array:

array = ["John", "Robert", :name, 557]

You access its values like this: array[0] will give you the result "John" and array[2] will give you :name. So those keys - numbers - are enforced.

In hashes, you define how the keys to those values look like, you can customize them and use anything you want:

hash = { "friend1" => "John", "friend2" => "Robert", :attribute => :name, :number_of_hours => 557, 1996 => "Best year!" }

You then access those values using your custom keys: hash["friend1"] gives you "John" and hash[1996] gives you "Best year!". The thing on the left of the => symbol is the key and the thing on the right is the value for that key.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Experiment in your IRB - you can even copy my code and then try accessing different elements of this array and hash.

Hi Maciej -

Thanks for your take on the use of arrays and hashes. Based on your explanation, I think I have a much better understanding of the two. The added customizaton seems like a real plus with hashes. I will experiment in the console with arrays and hashes to get more of a practical feel for how each works. I'm just getting started learning Ruby. Seems like both arrays and hashes are very powerful and incredibly useful. Eager to learn more.

Best,

Ronald

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You will later use hashes a lot in Rails (arrays not that much), so make sure you understand these bad boys ;). Also, understanding what symbols are will be useful (these are the :name things, without quotes).

Maciej -

Thanks for the heads-up about the importance of hashes and symbols in Ruby/Rails development. I learn best when I can relate the many pieces to the whole. So, it's good to know I'll be using a lot of hashes as I move forward in Ruby/Rails development.

On a related note.....

I find the "extra credit" exercises in Treehouse most useful. They require one to use the concepts to solve problems. More importantly, to solve the problem, you often need to do some outside research. Usually, the "answer or solution" isn't straight out of the video lesson. Those exercises help to translate the concepts to practice. That's a good thing.

Again, thanks for the tip.

Best regards,

Ronald