Ruby Ruby Loops Build a Simple Contact List Hash and Array Iteration

4Ctech Admin
4Ctech Admin
5,062 Points

Ruby: Stage 3: Build A Simple Contact List Challenge not working

I don't get why this code isn't working. I tried a few things that didn't work and then just used the same code that Jason Seifer used in the video. I don't really get an error. The response is just "Bummer! Try again!"

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

contact_list.each do |contact|
  puts "Name: #{contact["name"]}"
  if contact["phone_number"].size > 0
    contact["phone_number"].each do |number|
      puts "Phone Number : #{contact["phone_number"]}"
    end
  end
Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

My code looks exactly the same aside from my block choice.

It's frustrating when these challenges are deceptively simple.

I had to come here to the forum and see why I was getting the same error, so thanks for asking this question and helping me out!

3 Answers

Hi,

They require only values without additional information, just simple like this without complicated. :-)

contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]

contact_list.each do |contact|
  puts contact["name"]
  puts contact["phone_number"]
end

Hope that helps.

It's frustrating that most of the other answers allow things like

puts "Name: #{name}" and this one doesn't allow for anything extra.

sergiu cozma
sergiu cozma
14,774 Points

Why doesn't this code works?

contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]

contact_list.each do |contact|
  contact.each_value do |v|
    puts v
  end
end