Answer the question
In order to leave comments, you need to log in
`
How to fix undefined method `[]' for nil:NilClass?
There is an array
LINE = [
' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', ',', '.', ':', ';', '-'
]
$square = Hash.new
def build_square
LINE.each { |c| $square[c] = LINE.rotate(LINE.index(c)) }
end
build_square
def encode(text, key)
res = ""
(0..text.size).each do |i|
line = $square[key[i]]
n = LINE.index(text[i])
res += line[n]
end
res
end
Answer the question
In order to leave comments, you need to log in
line = $square[key[i]]
There are characters in the key string that are not in either $square or the LINE constant, as a result, at one of the iterations, the line variable is equal to nil
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question