Adding user input to a hash

J

John Maclean

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
 
L

Levin Alexander

T24gMi82LzA2LCBKb2huIE1hY2xlYW4gPGluZm9AamF5ZW9sYS5vcmc+IHdyb3RlOgoKPiAgICAg
aWYgdXNlcl9pbnB1dC5jaG9tcCEgIT0gInFxIgo+ICAgICAgIGFza19saXN0LmVhY2hfdmFsdWUg
PSB1c2VyX2lucHV0ICMgdGhpcyBsaW5lIGRvbid0IHdvcmsKCkhvdyBjb3VsZCBpdCwgeW91IGFy
ZSB0cnlpbmcgdG8gYXNzaWduIHRvIGEgbWV0aG9kLgoKVHJ5IHRoaXM6CgogIHF1ZXN0aW9ucyA9
ICV3e05hbWUgSm9ifQogIGFuc3dlcnMgPSBxdWVzdGlvbnMuaW5qZWN0KHt9KSB7IHxoYXNoLCBx
dWVzdGlvbnwKICAgIHByaW50ICIje3F1ZXN0aW9ufTogIgogICAgYW5zd2VyID0gZ2V0cy5jaG9t
cAogICAgYnJlYWsgaGFzaCBpZiBhbnN3ZXIgPT0gInEiCiAgICBoYXNoW3F1ZXN0aW9uXSA9IGFu
c3dlcgogICAgaGFzaAogIH0KICBwIGFuc3dlcnMKCi1MZXZpbgo=
 
J

John Maclean

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.

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
 
R

Robert Klemme

John said:
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
 
J

John Maclean

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?



John said:
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
 
R

Robert Klemme

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

A greenhand rubyist program -- ask for comments 1
user input timeout. 7
Problem getting user input 5
Chomping and stomping 16
hash 6
Module Global Hash 8
Passing info to classes and methods 4
Read a hash 5

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top