to_chr
函數應該返回加密數組,但轉換爲字符。我已經嘗試了很多東西,並將那些不起作用的東西評論出來。將數組數組轉換爲字符ruby
class Encrypt
def initialize(code, string)
@code = code
@string = string
@encrypted = []
end
def to_byte
@string.each_byte do |c|
@encrypted.push(c + @code)
end
print @encrypted
end
def to_chr
n = @encrypted.length
# n.times do |i|
# @encrypted.push(i.chr)
# end
print @encrypted[0].chr
# @encrypted.each do |x|
# @encrypted.push(x.chr)
# end
# print @encrypted
end
end
goop = Encrypt.new(2, "hello")
goop.to_chr
#=> in `to_chr': undefined method `chr' for nil:NilClass (NoMethodError)
@Stefan [這](https://github.com/Evan-Peterson/Encryption)是鏈接到GitHub repo,to_chr函數應該返回加密數組,但轉換爲字符。 –
那麼當你運行代碼時GitHub'@ encrypted'是空的,你實際上並沒有給它分配任何東西......所以也許從這開始(做'goop.to_byte'來填充數組)我也推薦'puts'而不是'print' –