0

Here is a hash

personal_details = {
  name: name,
  dob: dob,
  age: age,
  height: height
}

Here is an array

puts "What are the name of some of your relatives? (eg. Bob, James, Harry etc)"
relatives = gets().chomp.split(",")

Lets say the input for relatives was "Bob, James, Harry".

I want the hash to look like this:

personal_details = {
  name: name,
  dob: dob,
  age: age,
  height: height
  relatives: [
         {name: Bob},
         {name: James},
         {name: Harry}
  ]
}

The array in the hash is updated based on user input.

2 Answers 2

2

Start by initialising relatives as an array:

personal_details = {
  name: name,
  dob: dob,
  age: age,
  height: height,
  relatives: []
}

Then for each relative, push to the array:

relatives = gets().chomp.split(",")
relatives.each do |relative|
  personal_details.relatives.push(name: relative)
end
Sign up to request clarification or add additional context in comments.

1 Comment

This worked personal_details[:relatives].push(name: relative)
0

This should work, if I've interpreted the question as intended:

personal_details[:relatives] = relatives.map{|r| {name: r}}

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.