2014-04-12 14 views
1

我在項目中使用gem gmail來使用終端登錄帳戶; 操作系統是Linux(debian whezzy)。如何使用ruby中的gets/chomp登錄帳戶而不在終端顯示字符串?

以下是運行的代碼一個例子:

[email protected]:/home/user/project$ ruby --version 
**ruby 2.1.0p0 (2013-12-25 revision 44422) [i686-linux]** 

[email protected]:/home/user/project$ ruby login.rb 
=========== 
Gmail login 
=========== 
please give your username (without @gmail.com) and password 
(they will not be show, so please type first, press RET, type password) 
username: # waiting for username... 
password: # waiting for password... 

的問題是:使用gets.chomp,提示符顯示它的用戶名/密碼的字符串。我不想要這個;我希望有一種類似於UNIX系統帳戶登錄的功能,即提示等待輸入,但不顯示爲用戶輸入。

4篇一些參考文獻,我把使用的代碼:

def respond_to(&block) 
    puts "===========" 
    puts "Gmail login" 
    puts "===========" 

    puts "please give your username (without @gmail.com) and password (they will not be show, so please type first, press RET, type password)" 

    Gmail.connect("#{gets.chomp}@gmail.com", "#{gets.chomp}") do |client| 
    if client.logged_in? 
     puts "\n#{client.username} sucessfully logged in!\n" 
     yield(false, client) 
    else 
     true 
    end 
    end 

end 

我跟着一些提示在zetcode,但不具有光的射線。

謝謝!

回答

1

有幾個寶石,可以做你想做的 - 其中之一是password

紅寶石/密碼是密碼處理程序爲Ruby, 包括接口Cracklib的爲目的的集合測試 密碼強度。

require 'password' 

# Define and check a password in code 
pw = Password.new("bigblackcat") 
pw.check 

# Get and check a password from the keyboard 
begin 
    password = Password.get("New password: ") 
    password.check 
rescue Password::WeakPassword => reason 
    puts reason 
    retry 
end 
相關問題