Blog of Jeff A blog about programming and random other things.

2Mar/09link

Password Input in Ruby?

A recent hurdle I can't seem to solve involves request a password from the user (via the command line). Ideally, it would mimic the typical unix-style of hiding all characters being entered, however Ruby seems to falter here.

As far as I'm aware of, Ruby natively doesn't provide a password input feature [another reason to like python ;) ] but a popular third-party library for this is highline as so:

require 'rubygems'
require 'highline/import'

password = ask("Enter password: ") { |q| q.echo = false }
# do stuff with password

There seems to be an unfortunate flaw when using cygwin though.

Highline

As you can see, an arbitrary amount of input still seeps through to stdout. Either there's fault to cygwin's rxvt or ruby in general. I'm guessing it's more likely the former since the command prompt doesn't seem to have any trouble:

cmdline_bash

Oddly enough, python's getpass module has no problems at all in either.

Hopefully after midterms I'll get a chance to write about my "get acquainted with the newer version of rails" project afterwards :)

  • Share/Bookmark
blog comments powered by Disqus

Recent Posts

Topics

Archives

Following

Links