gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:55 24 4
gpt4 key购买 nike

我的 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

我尝试运行文件后在 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\>

最佳答案

这里有两个问题:

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

kee = $stdin.gets.chomp

关于错误信息,是因为:

The key is anywhere between 64 and 448 bits (8 and 56 bytes), and should be passed as a packed string

关于Ruby 脚本不等待命令提示符中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14753059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com