Ruby脚本提示输入密码并让用户再次输入密码进行验证

时间:2014-11-17 15:13:53

标签: ruby

我编写了一个ruby脚本,要求输入密码并稍后将其分配给变量。我希望能够再次要求用户输入密码进行验证。

这就是我现在所拥有的,我只是不确定如何再次询问密码以进行验证。

#!/usr/bin/ruby
#
require 'rubygems'
require 'highline/import'

def getPassword(prompt)
  loop do
    word = ask("#{prompt}") { |x| x.echo = "*" }
    if word.nil? or word.empty?
     puts 'Password cannot be blank.'
    else
     return word
     break
    end
  end
end

user_password = getPassword('Enter User Password')

1 个答案:

答案 0 :(得分:0)

这是我改变的:

#!/usr/bin/ruby
#
require 'rubygems'
require 'highline/import'

def getPassword(prompt)
  loop do
    word = ask("#{prompt}") { |x| x.echo =  "*" }
    verify = ask("#{prompt} Again") { |z| z.echo =  "*" }
     if word != verify
       puts "They do not match"
     else
       puts "They Match"
       return word
       break
     end
  end
end

user_password = getPassword('Enter User Password')
相关问题