From: John Maclean Date: 2006-02-07T21:14:34+09:00 Subject: Re: Adding user input to a hash Thanks for that reply. I think that I will go for the two array method. One for the input and the other to save the data. On a side-note but related issue I'd like to save this data to a file. The name of the filename to be created is based on the user input.... jayeola@tp20$ cat testwrite_using_filename.rb #!/usr/bin/ruby -wv def testwrite_using_array ask_list = [ 'Your name', 'Your stuff', 'Your foo', ] array_counter = 0 user_response = [] ask_list.each { |z| puts "#{z} : " xx = gets.chomp if xx.chomp! != "qq" array_counter += 1 user_response += "#{xx}".to_a # puts array_counter # check number of elements in array end } puts user_response # name of next variable used user respose....we need to save this data to a file name like "name + age + foo".txt filename =user_response[1..3].to_s puts "filename #{filename}.txt created" File.open(filename, "a") { "#{user_response}" } end testwrite_using_array # So far the file is created, but nothing's in it - it's empty...I know I'll get there. Just need a few pointers. Any ideas chaps? On Mon, 6 Feb 2006 21:43:21 +0900 "Robert Klemme" wrote: > John Maclean wrote: > > Chaps, > > > > Say you've got an hash that you are using to capturing user > > input.... nearly there:- > > > > #!/usr/bin/ruby -w > > def testwrite > > ask_list = { > > 'Your name' => 'Nil', > > 'Your dob' => 'Nil', > > } > > ask_list.each_key { > > |z| puts "#{z} : " > > user_input = gets.chomp > > if user_input.chomp! != "qq" > > ask_list.each_value = user_input # this line don't work > > end > > } > > end > > > > testwrite > > I'd choose a completely different design: I'd have questions in a list > (Arry) and answers in another list (Array) or Hash. > > You could do: > > questions = [ > 'Your name', > 'Your job', > ] > > answers = questions.map |q| > puts q > gets.chomp > end > > Reason is that your question list is likely constant over time and > your answers depend on each run. If you put your original solution > into a loop for different users then you end up having old answers > from someone still in the hash. Concurrency won't work either. > > Kind regards > > robert > > > > -- John Maclean MSc (DIC) 07739 171 531