tags:

views:

41

answers:

2

I want to ask users to type in a password, but i dont want the chars to appear on screen as they type.

How do I do this in Ruby?

Thanks

+5  A: 

There is a gem for such user interaction: highline.

password = ask("Password:  ") { |q| q.echo = false }

Or even:

password = ask("Password:  ") { |q| q.echo = "*" }
Konstantin Haase
sweet! (15chars)
st0le
Thanks, although thats a rather bloated solution. Think i'll stick with \e[30m to simply make the foreground black.
Andrew Bullock
What if the user's terminal does not have a black background?
glenn jackman
its only for an in-house script, so its not an issue :) good to point out for others though
Andrew Bullock
It will still show up in logs and such.
Konstantin Haase
+3  A: 

If you're on a system with stty:

`stty -echo`
print "Password: "
pw = gets.chomp
`stty echo`
puts ""
glenn jackman