Adding user input to a hash


#1

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


#2

On 2/6/06, John M. removed_email_address@domain.invalid 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.

Try this:

questions = %w{Name Job}
answers = questions.inject({}) { |hash, question|
print "#{question}: "
answer = gets.chomp
break hash if answer == “q”
hash[question] = answer
hash
}
p answers

-Levin


#3

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 A.


#4

John M. wrote:

          }

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

#5

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

John M. wrote:

          '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.

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.
:slight_smile:

File.open(filename, “a”) {|io| io.puts answers}

HTH

robert

#7

On 6-Feb-06, at 6:16 AM, John M. wrote:

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

Do you mean

ask_list[z] = user_input

here?

end

}
end

testwrite

Mike

Mike S. removed_email_address@domain.invalid
http://www.stok.co.uk/~mike/

The “`Stok’ disclaimers” apply.