How to hide password input from terminal in ruby script

前端 未结 6 1763
囚心锁ツ
囚心锁ツ 2020-11-30 20:50

I am new to ruby. I need to receive password as an input through gets command.

How do I hide the password input typed in the terminal, during gets

6条回答
  •  有刺的猬
    2020-11-30 21:05

    One can also use core ruby.

    $ ri IO.noecho
    
     (from ruby core)
     ------------------------------------------------------------------------------
       io.noecho {|io| }
      ------------------------------------------------------------------------------
    
     Yields self with disabling echo back.
    
       STDIN.noecho(&:gets)
    
     will read and return a line without echo back.
    

    For 1.9.3 (and above), this requires you adding require 'io/console' to your code.

    require 'io/console'
    text = STDIN.noecho(&:gets)
    

提交回复
热议问题