gpt4 book ai didi

ruby - 为什么当我尝试转换为 int 时 ruby​​ 会抛出错误?

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:07 25 4
gpt4 key购买 nike

我正在尝试使用 ruby​​ 制作一个简单的加密算法。

当我使用 .to_i 转换为 int 时,ruby 打印“没有从 C:/Users/liam/Downloads/encryption.rb:60:in '' 将字符串隐式转换为 Integer(TypeError)”。

第 60 行如下:

cleartext.to_i

剩下的代码是:

print ">"
cleartext = gets.chomp
print ">"
symetricKey = gets.chomp
puts "cleartext and symetricKey obtained. calculating result..."
STDOUT.flush
symetricKey.gsub! 'a', '1'
symetricKey.gsub! 'b', '2'
symetricKey.gsub! 'c', '3'
symetricKey.gsub! 'd', '4'
symetricKey.gsub! 'e', '5'
symetricKey.gsub! 'f', '6'
symetricKey.gsub! 'g', '7'
symetricKey.gsub! 'h', '8'
symetricKey.gsub! 'i', '9'
symetricKey.gsub! 'j', '10'
symetricKey.gsub! 'k', '11'
symetricKey.gsub! 'l', '12'
symetricKey.gsub! 'm', '13'
symetricKey.gsub! 'n', '14'
symetricKey.gsub! 'o', '15'
symetricKey.gsub! 'p', '16'
symetricKey.gsub! 'q', '17'
symetricKey.gsub! 'r', '18'
symetricKey.gsub! 's', '19'
symetricKey.gsub! 't', '20'
symetricKey.gsub! 'u', '21'
symetricKey.gsub! 'v', '22'
symetricKey.gsub! 'w', '23'
symetricKey.gsub! 'x', '24'
symetricKey.gsub! 'y', '25'
symetricKey.gsub! 'z', '26'
symetricKey.to_i
cleartext.gsub! 'a', '1'
cleartext.gsub! 'b', '2'
cleartext.gsub! 'c', '3'
cleartext.gsub! 'd', '4'
cleartext.gsub! 'e', '5'
cleartext.gsub! 'f', '6'
cleartext.gsub! 'g', '7'
cleartext.gsub! 'h', '8'
cleartext.gsub! 'i', '9'
cleartext.gsub! 'j', '10'
cleartext.gsub! 'k', '11'
cleartext.gsub! 'l', '12'
cleartext.gsub! 'm', '13'
cleartext.gsub! 'n', '14'
cleartext.gsub! 'o', '15'
cleartext.gsub! 'p', '16'
cleartext.gsub! 'q', '17'
cleartext.gsub! 'r', '18'
cleartext.gsub! 's', '19'
cleartext.gsub! 't', '20'
cleartext.gsub! 'u', '21'
cleartext.gsub! 'v', '22'
cleartext.gsub! 'w', '23'
cleartext.gsub! 'x', '24'
cleartext.gsub! 'y', '25'
cleartext.gsub! 'z', '26'
cleartext.to_i
ciphertext = cleartext * (symetricKey * symetricKey * 100)
ciphertext.to_s
ciphertext.gsub! 'z', '26'
ciphertext.gsub! 'y', '25'
ciphertext.gsub! 'x', '24'
ciphertext.gsub! 'w', '23'
ciphertext.gsub! 'v', '22'
ciphertext.gsub! 'u', '21'
ciphertext.gsub! 't', '20'
ciphertext.gsub! 's', '19'
ciphertext.gsub! 'r', '18'
ciphertext.gsub! 'q', '17'
ciphertext.gsub! 'p', '16'
ciphertext.gsub! 'o', '15'
ciphertext.gsub! 'n', '14'
ciphertext.gsub! 'm', '13'
ciphertext.gsub! 'l', '12'
ciphertext.gsub! 'k', '11'
ciphertext.gsub! 'j', '10'
ciphertext.gsub! 'i', '9'
ciphertext.gsub! 'h', '8'
ciphertext.gsub! 'g', '7'
ciphertext.gsub! 'f', '6'
ciphertext.gsub! 'e', '5'
ciphertext.gsub! 'd', '4'
ciphertext.gsub! 'c', '3'
ciphertext.gsub! 'b', '2'
ciphertext.gsub! 'a', '1'
puts (ciphertext)

最佳答案

你还没有分配你的结果

to_ito_s 将返回您需要分配回变量的结果,如下所示。

这样赋值

cleartext=cleartext.to_i

ciphertext=ciphertext.to_s

symetricKey=symetricKey.to_i

试试这个

print ">"
cleartext = gets.chomp
print ">"
symetricKey = gets.chomp
puts "cleartext and symetricKey obtained. calculating result..."
STDOUT.flush
symetricKey.gsub! 'a', '1'
symetricKey.gsub! 'b', '2'
symetricKey.gsub! 'c', '3'
symetricKey.gsub! 'd', '4'
symetricKey.gsub! 'e', '5'
symetricKey.gsub! 'f', '6'
symetricKey.gsub! 'g', '7'
symetricKey.gsub! 'h', '8'
symetricKey.gsub! 'i', '9'
symetricKey.gsub! 'j', '10'
symetricKey.gsub! 'k', '11'
symetricKey.gsub! 'l', '12'
symetricKey.gsub! 'm', '13'
symetricKey.gsub! 'n', '14'
symetricKey.gsub! 'o', '15'
symetricKey.gsub! 'p', '16'
symetricKey.gsub! 'q', '17'
symetricKey.gsub! 'r', '18'
symetricKey.gsub! 's', '19'
symetricKey.gsub! 't', '20'
symetricKey.gsub! 'u', '21'
symetricKey.gsub! 'v', '22'
symetricKey.gsub! 'w', '23'
symetricKey.gsub! 'x', '24'
symetricKey.gsub! 'y', '25'
symetricKey.gsub! 'z', '26'
symetricKey=symetricKey.to_i
cleartext.gsub! 'a', '1'
cleartext.gsub! 'b', '2'
cleartext.gsub! 'c', '3'
cleartext.gsub! 'd', '4'
cleartext.gsub! 'e', '5'
cleartext.gsub! 'f', '6'
cleartext.gsub! 'g', '7'
cleartext.gsub! 'h', '8'
cleartext.gsub! 'i', '9'
cleartext.gsub! 'j', '10'
cleartext.gsub! 'k', '11'
cleartext.gsub! 'l', '12'
cleartext.gsub! 'm', '13'
cleartext.gsub! 'n', '14'
cleartext.gsub! 'o', '15'
cleartext.gsub! 'p', '16'
cleartext.gsub! 'q', '17'
cleartext.gsub! 'r', '18'
cleartext.gsub! 's', '19'
cleartext.gsub! 't', '20'
cleartext.gsub! 'u', '21'
cleartext.gsub! 'v', '22'
cleartext.gsub! 'w', '23'
cleartext.gsub! 'x', '24'
cleartext.gsub! 'y', '25'
cleartext.gsub! 'z', '26'
cleartext=cleartext.to_i
ciphertext = cleartext * (symetricKey * symetricKey * 100)
ciphertext=ciphertext.to_s
ciphertext.gsub! 'z', '26'
ciphertext.gsub! 'y', '25'
ciphertext.gsub! 'x', '24'
ciphertext.gsub! 'w', '23'
ciphertext.gsub! 'v', '22'
ciphertext.gsub! 'u', '21'
ciphertext.gsub! 't', '20'
ciphertext.gsub! 's', '19'
ciphertext.gsub! 'r', '18'
ciphertext.gsub! 'q', '17'
ciphertext.gsub! 'p', '16'
ciphertext.gsub! 'o', '15'
ciphertext.gsub! 'n', '14'
ciphertext.gsub! 'm', '13'
ciphertext.gsub! 'l', '12'
ciphertext.gsub! 'k', '11'
ciphertext.gsub! 'j', '10'
ciphertext.gsub! 'i', '9'
ciphertext.gsub! 'h', '8'
ciphertext.gsub! 'g', '7'
ciphertext.gsub! 'f', '6'
ciphertext.gsub! 'e', '5'
ciphertext.gsub! 'd', '4'
ciphertext.gsub! 'c', '3'
ciphertext.gsub! 'b', '2'
ciphertext.gsub! 'a', '1'
puts (ciphertext)

更新:正如@engineersmnky 提到的

symetricKey = symetricKey.gsub(/[a-z]/) { |letter| letter.ord - 96 }.to_i

关于ruby - 为什么当我尝试转换为 int 时 ruby​​ 会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979474/

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