JEG2 / highline

A higher level command-line oriented interface.
Other
1.29k stars 137 forks source link

repeat_entry.rb example's hash gather repeats the original question twice, not using elements of the hash #87

Closed daniel-ferguson closed 11 years ago

daniel-ferguson commented 11 years ago

Cut down code from examples/repeat_entry.rb

require 'rubygems'
require 'highline/import'
pass = ask("Enter your password:  ") do |q|
  q.echo = '*'
  q.verify_match = true
  q.gather = {"Enter a password" => '',
              "Please type it again for verification" => ''}
end

response from terminal:

Enter your password:  ****
// I expected: "Enter a password: "
Enter your password:  ****      
// I expected: "Please type it again for verification: "
Your password is now asdf!

I feel like I might be missing something in terms of usage that isn't correctly illustrated by the example.

JEG2 commented 11 years ago

The example is a little odd. The question is ignored when gather with a Hash is used. See the description of gather in the RDoc.

daniel-ferguson commented 11 years ago

Mmmm.. That's what I expected. I've had a look at that already.

"Optionally gather can be set to a Hash. In this case, the question will be asked once for each key and the answers will be returned in a Hash, mapped by key. The @key variable is set before each question is evaluated, so you can use it in your question."

And the code below makes me think that I should ask() differently if I'm using a hash. Though it seems that it's locked into asking the original question, however many times.

# /lib/highline.rb 
# lines: 783 - 790
when Hash
  @answers = { }
  @gather.keys.sort.each do |key|
    @question     = original_question
    @key          = key
    @answers[key] = ask(@question)
end

Is it possible that you could give me a super simple example of gather working with a hash, I'm not familiar with highline yet and the RDoc entry didn't lead me to a working solution. I'm sure it's something simple. Thank you.

JEG2 commented 11 years ago

I think you are right and I am wrong. It looks like this is a bug. It's not working as it should. :frowning: