Ruby $stdin.gets without showing chars on screen

前端 未结 6 2063
北海茫月
北海茫月 2020-12-30 00:21

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?

6条回答
  •  萌比男神i
    2020-12-30 01:05

    There is a gem for such user interaction: highline.

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

    Or even:

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

提交回复
热议问题