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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -

Why does a .NET 4.0 program produce a system.unauthorizedAccess error on a Windows Server 2012 machine with .NET 4.5 installed? -