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.
whoa! Thanks. It works but asa novice to this language I would never
have known of questions.inject({}) { |hash, question| ... pouring over
this line and ri Hash.
···
On Mon, 6 Feb 2006 20:32:13 +0900 Levin Alexander <levin@grundeis.net> wrote:
On 2/6/06, John Maclean <info@jayeola.org> wrote:
> if user_input.chomp! != "qq"
> ask_list.each_value = user_input # this line don't work
How could it, you are trying to assign to a method.
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" <bob.news@gmx.net> 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.
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....
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.