You create instance of Encrypted
method, but you set @code = 2
, @string = "Hello"
and @encrypted = []
. So if you call @encrypted[0]
, ruby return nil
.
So you can modify your class like this:
class Encrypt
def initialize(code, string)
@code, @string, @encrypted = code, string, []
end
def to_byte
@string.each_byte { |c| @encrypted << c + @code }
end
def to_chr
to_byte if @encrypted.empty?
@encrypted.map(&:chr)
end
end
goop = Encrypt.new(2, "hello")
p goop.to_chr
# => ["j", "g", "n", "n", "q"]
I hope this helps
solved Converting an array of numbers to characters ruby [closed]