# String+char, cant convert nil, but not nil

**URL:** <https://www.ruby-forum.com/t/string-char-cant-convert-nil-but-not-nil/138716>\
**Category:** Ruby\
**Created:** [May 27, 2008, 6:31am UTC](https://www.ruby-forum.com/t/string-char-cant-convert-nil-but-not-nil/138716 "2008-05-27T06:31:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 27, 2008, 6:31am UTC](https://www.ruby-forum.com/t/string-char-cant-convert-nil-but-not-nil/138716/1 "2008-05-27T06:31:58Z")

</div>

tryong to do a ROT13([ROT13 - Wikipedia](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…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 27, 2008, 6:58am UTC](https://www.ruby-forum.com/t/string-char-cant-convert-nil-but-not-nil/138716/2 "2008-05-27T06:58:53Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# tryong to do a ROT13([ROT13 - Wikipedia](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

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 🙂

# 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
