0

I need to use a loop in my code so the user is prompted with "Name?" three times, and each answer is stored as a new hash within the data array. Each answer should also have a new random number generated for it, and an email.

I need puts data to output all three hashes and their contents. I've tried using 3.times do, but I'm having trouble:

data = Array.new()

puts "Name?, eg. Willow Rosenberg"
name = gets.chomp
number = rand(1000..9000) + 1
    data = [
        {
        name: name,
        number: number,
        email: name.split(' ').last + number.to_s[1..3] + "@btvs.com"
        }
    ]

puts data

1 Answer 1

1
data = []

3.times do
  puts "Name?, eg. Willow Rosenberg"
  name = gets.chomp
  number = rand(1000..9000) + 1

  hash = {
    name: name,
    number: number,
    email: name.split(' ').last + number.to_s[1..3] + "@btvs.com"
  }

  data << hash
end

puts data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.