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

What does it mean... "Assign"

I don't know how to Assign array items to a variable.

contact_list.rb
contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]
contact_list.each do |item|

1 Answer

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

Hi Zachary,

The variable on the "slide" (i.e. "|item|") is the named variable used to access the element within the block.

For example:

contacts = ['Bob', 'Jane', 'Sue']

contacts.each do |contact|
  puts contact
end

would return

Bob
Jane
Sue

An alternative syntax for this, which avoids the do...end syntax would be:

contacts = ['Bob', 'Jane', 'Sue']

contacts.each { |contact| puts contact }

Hope that helps!

I don't understand any of the challenge... I really just need the answer, so i can work back from it...

and why does it keep disconnecting while I'm doing the challenge, then have to restart?