How to split a string, iterate over it, and output a joined version
<p>I'm working on a code for <code>caesar_cipher</code>. I am writing a method for encryption without making a <code>Caesar</code> class and defining other methods inside of it. The code I wrote is below:</p><pre><br /><code>def caesar(string, shift = 0)<br /> alphabet = ("a".."z").to_a<br /> letters = string.split("")<br /> blank = []<br /> letters.map do |letter|<br /> blank << alphabet[(alphabet.index(letter) + shift) % alphabet.length] <br /> end <br /> puts blank.join<br />end<br /></code><br /></pre><p>I get this error when putting in a string consisting of more than one word:</p><pre><br /><code>block in `caesar_cipher': undefined method `+' for nil:NilClass (NoMethodError)<br /></code><br /></pre><p>I understand <code>tr</code> can solve my problem if I were to create the class. I have an independent method that works on every angle of <code>caesar_cipher</code> except sentences. I cannot get the words to be split, iterate, and then join. Any input will help me avoid this.</p><br /><h2>Click here view more answers <a href="http://ift.tt/1UjFozv">http://ift.tt/1UjFozv</a></h2>
Nhận xét
Đăng nhận xét