ruby脚本gets方法没有接受输入
怪我咯
怪我咯 2017-04-24 09:09:27
0
1
661

执行这个脚本的时候,kee=gets.chomp这行代码没有接受输入,请问是怎么回事?谢谢。

#!/usr/bin/ruby

require 'crypt/blowfish'

unless ARGV[0]
    puts "Usage: ruby encrypt.rb <filename.ext>"
    puts "Example: ruby encrypt.rb secret.stuff"
    exit
end

filename=ARGV[0].chomp
puts filename
c="Encrypted_#{filename}"
if File.exists?(c)
    puts "File already exists."
    exit
end
print "Enter your encryption key (1-56 bytes): "
kee=gets.chomp
begin
    blowfish=Crypt::Blowfish.new(kee)
    blowfish.encrypt_file(filename.to_str,c)
    puts 'Encryption sucess!'
rescue Exception => e
    puts "An error occurred during encryption: \n #{e}"
end
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(1)
洪涛

你呼叫的是Kernel裡面的gets方法,這個gets方法會試著去讀取ARGV裡面的內容,除非AGRV為空,這時才試著讀取STDIN的內容。

http://www.ruby-doc.org/core-2.2.0/Kernel.html#method-i-gets

你應該呼叫的是IO#gets,例如STDIN.gets而不是gets

http://www.ruby-doc.org/core-2.2.0/IO.html#method-i-gets

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!