0

Im trying to store all of User's input into a hash and then loop through the hash and display results. Input: First name, Last Name, Age, City Visited(user will input multiple cities until they input "exit".

Here's what I got so far..and everything looks good except being able to separate the cities as multiple Values when multiple cities are entered.

result = ""
print "Enter first name "
first = gets.chomp
print "Enter last name "
last = gets.chomp
print "Enter age "
age = gets.chomp

while TRUE
print "Enter city"
city = gets.chomp
if city == "exit"
  break
end
result = result + " " + city
end

user_data = { first: first, last: last, age: age, city: result}

puts "#{user_data}"
0

1 Answer 1

1

It's better to use an array for this purpose, like this:

cities = []

loop do
  print "Enter city: "
  city = gets.chomp
  if city == "done"
    break
  end
  cities << city
end

user_data = { first: first, last: last, age: age, city: cities}

And after that you can make a string representation by joining the elements of the array, for example

cities.join(' ')
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.