Method in Ruby to replace string with characters in the alphabet + index 13
<p>From a string, I am trying to replace each character with another character, 13 letters ahead in the alphabet. For example, &quot;ABC&quot; would return &quot;NOP&quot;.</p>
<p>However, when I get to the end of the alphabet, I can't find a way to loop the index, so that it goes from index <code>[-1]</code> to <code>[0]</code> again and onwards. For example - When I type in &quot;XYZ&quot; it returns &quot;&quot;, but it should return &quot;KLM&quot;.</p>
<p>Can anyone think of a solution to this?</p>
<pre><code>def rot13(string)   
alphabet = (&quot;a&quot;..&quot;z&quot;).to_a   
big_alphabet = (&quot;A&quot;..&quot;Z&quot;).to_a   
result = []   
split_string = string.chars   
alphabet.each_with_index do |item,index|
        if string.include?(alphabet[index])
          result &lt;&lt; alphabet[index+13]
        elsif string.include?(big_alphabet[index])
          result &lt;&lt; big_alphabet[index+13]   
end 
end 
return result.join 
end
    
</code></pre>