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 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