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 Loops Build a Simple Contact List Hash and Array Iteration

could this question be explained in other words that would be great.

please explain

contact_list.rb
contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]
Mike Hickman
Mike Hickman
19,817 Points

Hi Henry,

Did you get this figured out?

Mike

sorry. I forgot to write the instructions. No I did not figure it out. It is from the ruby loops course I think.

3 Answers

Garret Saarinen
Garret Saarinen
3,942 Points

Got it. I wasn't sure what you were asking about.

For this exercise, it wants you to iterate over each element in the array so using the each method

contact_list.each

and assign each element in the array to a block variable called contact

contact_list.each do |contact|

The next part of printing out the values for each element is tricky because each element in the array is a hash with keys and values. The syntax needs to have a little more nuance because you are only supposed to print out the values so you need to pass in the key-

contact_list.each do |contact|
   puts "Name: #{contact["name"]} PH: #{contact["phone_number"]}"
end

The contact part in the puts statement is block variable that is representing each hash in the contact_list array. Because it's a hash you have to add the ["name"] to indicate you want the value for just that key.

Hope that helps.

Garret Saarinen
Garret Saarinen
3,942 Points

An array is like a container or list holding a certain number of things.

Big Jar

  • marble
  • block

etc.

In this example, each "thing" inside the Big Jar (array) is a hash ( denoted by the { } symbols ) that just contains some more specific information about itself.

So the example above could be written more like this:

Big Jar [

  • {"name" => "marble", "shape" => "sphere"},
  • {"name" => "block", "shape" => "cube"}

]

or

big_jar = [
{"name" => "marble", "shape" => "sphere"},
{"name" => "block", "shape" => "cube"}
]

Hope that helps! `

I'm aware what an array is and what a hash is but the challenge instructions are not clear. If someone could explain the instructions to me that would help or else ill just have to ignore this challenge.