string+char, cant convert nil, but not nil

N

notnorwegian

tryong to do a ROT13(http://en.wikipedia.org/wiki/Rot_13) 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...
 
P

Peña, Botp

From: (e-mail address removed) [mailto:[email protected]]=20
# tryong to do a ROT13(http://en.wikipedia.org/wiki/Rot_13) encrypter/
# decrypter.
# x =3D "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# y =3D "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
# arr =3D x.split(//).zip(y.split(//))
# dict =3D Hash[*arr.flatten]
# puts "Input phrase: "
# phrase =3D gets

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

# rotated =3D ""
# for x in (0..phrase.length()-1)
# rotated =3D rotated + dict[phrase[x].chr]
# end
# puts rotated
#=20
# '+' cant convert nil into string
# cant convert nil into string

that's the effect

dict["\n"]
#=3D> 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')
#=3D> "grfgvat"

kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top