String+char, cant convert nil, but not nil

tryong to do a ROT13(ROT13 - Wikipedia) encrypter/
decrypter.

x = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
y = “NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm”
arr = x.split(//).zip(y.split(//))
dict = Hash[*arr.flatten]

puts "Input phrase: "
phrase = gets

rotated = “”
for x in (0…phrase.length()-1)
rotated = rotated + dict[phrase[x].chr]
end
puts rotated

‘+’ cant convert nil into string
cant convert nil into string

puts dict[phrase[2].chr] works though…

From: [email protected] [mailto:[email protected]]

tryong to do a ROT13(ROT13 - Wikipedia) encrypter/

decrypter.

x = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”

y = “NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm”

arr = x.split(//).zip(y.split(//))

dict = Hash[*arr.flatten]

puts "Input phrase: "

phrase = gets

if you tried this in irb, you’d see the value of phrase.
hint: gets also stores the (unwanted) ending char
(chomp the newline pls :slight_smile:

rotated = “”

for x in (0…phrase.length()-1)

rotated = rotated + dict[phrase[x].chr]

end

puts rotated

‘+’ cant convert nil into string

cant convert nil into string

that’s the effect

dict[“\n”]
#=> nil

btw, in ruby, there are many ways to skin that rot13 problem.

eg,

“testing”.tr(‘A-MN-Za-mn-z’,‘N-ZA-Mn-za-m’)
#=> “grfgvat”

kind regards -botp