Simon Knights Simon Knights - 1 month ago 6x
Ruby Question

How can I read a password from the command line in Ruby?

I am running Ruby and MySQL on a Windows box.

I have some Ruby code that needs to connect to a MySQL database a perform a select. To connect to the database I need to provide the password among other things.

The Ruby code can display a prompt requesting the password, the user types in the password and hits the Enter key. What I need is for the password, as it is typed, to be displayed as a line of asterisks.

How can I get Ruby to display the typed password as a line of asterisks in the 'dos box'?


To answer my own question, and for the benefit of anyone else who would like to know, there is a Ruby gem called HighLine that you need.

require 'rubygems'
require 'highline/import'

def get_password(prompt="Enter Password")
   ask(prompt) {|q| q.echo = false}

thePassword = get_password()