From: Robert Klemme Date: 2006-02-07T21:23:21+09:00 Subject: Re: Adding user input to a hash John Maclean wrote: > 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', > ] Better put questions in a global constant. Otherwise you'll always recreate the array on each invocation of testwrite_using_array(). > array_counter = 0 > user_response = [] > ask_list.each { > |z| puts "#{z} : " > xx = gets.chomp > if xx.chomp! != "qq" There's no point in chompig the same string twice. > array_counter += 1 You don't need array_counter because the array knows its size. > user_response += "#{xx}".to_a You rather want: user_responses << xx Cleaner, easier and faster. > # 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? You don't write to the file - as a consequence there is nothing in there. :-) File.open(filename, "a") {|io| io.puts answers} HTH robert