Ruby Ruby Loops Build a Simple Contact List Part 1: Asking Questions

Gennadiy Anikeev
Gennadiy Anikeev
2,559 Points

Could you give me some advices about my code?

I just tried to write my own version before watching the videos, and it would be great to hear some review from more experienced programmer. Here is the code:

#creating methods:
def get_name
  print "Enter the person's name: "
  name = gets.chomp
  return name
end

def get_number
  array = []
  loop do
    print "Do you want to add a number?(y/n) "
    answer = gets.chomp.downcase
    if answer == "y"
      print "Enter the number: "
      number = gets.chomp
      array.push(number)
    else
      break
    end
  end
  return array
end

def line
  puts "-" * 40
end

list = {}

#asking the user:
loop do
  print "Do you want to add a person?(y/n) "
  answer = gets.chomp.downcase
  if answer == "y"
    list.store(get_name, get_number)
  else 
    break
  end
end

#putting to the screen:
list.each do |name, numbers|
  puts "Name: " + name
  puts "Numbers: "
  numbers.each do |num|
    puts num
  end
  line
end