Storing user input into hash RUBY -
im trying store of user's input hash , loop through hash , display results. input: first name, last name, age, city visited(user input multiple cities until input "exit".
here's got far..and looks except being able separate cities multiple values when multiple cities 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}"
it's better use array purpose, this:
cities = [] loop 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 can make string representation joining elements of array, example
cities.join(' ')
Comments
Post a Comment