Forum: Ruby Adding user input to a hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-06 12:18
(Received via mailing list)
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
0d298cda3121e5cacaa2465437769025?d=identicon&s=25 Levin Alexander (Guest)
on 2006-02-06 12:33
(Received via mailing list)
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.

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
Cb7d2cb319c70e49fa1522614980e128?d=identicon&s=25 Mike Stok (Guest)
on 2006-02-06 12:33
(Received via mailing list)
On 6-Feb-06, at 6:16 AM, John Maclean 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 Stok <mike@stok.co.uk>
http://www.stok.co.uk/~mike/

The "`Stok' disclaimers" apply.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-06 12:54
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-06 13:46
(Received via mailing list)
John Maclean 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
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-07 13:16
(Received via mailing list)
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,
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-07 13:25
(Received via mailing list)
John Maclean 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.
:-)

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

HTH

    robert
This topic is locked and can not be replied to.