在编写交互程序时,如果在终端提示用户输入密码,当用户输入时,密码也会以明文同时输出到终端上。
因为STDIN会把所有的输入内容echo出来。
我找了一下有个RAA-Termios的库可以伪造一个STDIN,然后把输入进行截获。
但是这个库只能在linux下使用,win32下我就无能为力了。
大家遇到过这个问题吗?
用highline这个gem就行。
[code="command prompt"]gem install highline[/code]
看这段irb session:
[code="irb"]C:\Users\RednaxelaFX>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'highline/import'
=> true
irb(main):003:0> ask('enter password> ') {|c| c.echo = false }
enter password>
=> "asdf"
irb(main):004:0> quit
C:\Users\RednaxelaFX>[/code]
里面的ask方法就够用了