Ruby脚本不会在命令提示符中等待输入

use*_*619 1 ruby command-prompt

我的ruby脚本似乎不等我的输入.我正在尝试按照从本书第一章加密文件的脚本:wicked cool ruby​​,但是在命令提示符下运行文件后它不会等待我的输入.

导致问题的代码段:

require 'crypt/blowfish'

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

#take in the file name to encrypt as an argument
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 SUCCESS!'
rescue Exception => e
   puts "An error occurred during encryption: \n #{e}"
end
Run Code Online (Sandbox Code Playgroud)

我尝试运行文件后在cmd中看到的内容:

C:\Users\me\>ruby new4.rb fileToEnrypt.txt
Enter your encryption key (1-56 bytes): An error occurred during encryption:
 can't convert String into Integer
C:\Users\me\>
Run Code Online (Sandbox Code Playgroud)

ByS*_*pts 7

你有两个问题:

关于您没有收到用户提示的事实,请将代码替换为:

kee = $stdin.gets.chomp
Run Code Online (Sandbox Code Playgroud)

关于错误消息,这是因为:

密钥位于64到448位(8到56字节)之间,应作为压缩字符串传递