Guest User

Untitled

a guest
Oct 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. class CaesarCipher
  2.  
  3. def initialize(string, rot=13)
  4. @string = string
  5. @rot = rot
  6. end
  7.  
  8. def perform
  9. small_alphabet = [*"a".."z"]
  10. big_alphabet = [*"A".."Z"]
  11. string = @string.split("")
  12. string.each.with_index do |letter, index|
  13. if small_alphabet.include?(letter)
  14. position = (small_alphabet.index(letter) + @rot) % 26
  15. string[index] = small_alphabet[position]
  16. elsif big_alphabet.include?(letter)
  17. position = (big_alphabet.index(letter) + @rot) % 26
  18. string[index] = big_alphabet[position]
  19. end
  20. end
  21. string.join("")
  22. end
  23. end
Add Comment
Please, Sign In to add comment