Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CaesarCipher
- def initialize(string, rot=13)
- @string = string
- @rot = rot
- end
- def perform
- small_alphabet = [*"a".."z"]
- big_alphabet = [*"A".."Z"]
- string = @string.split("")
- string.each.with_index do |letter, index|
- if small_alphabet.include?(letter)
- position = (small_alphabet.index(letter) + @rot) % 26
- string[index] = small_alphabet[position]
- elsif big_alphabet.include?(letter)
- position = (big_alphabet.index(letter) + @rot) % 26
- string[index] = big_alphabet[position]
- end
- end
- string.join("")
- end
- end
Add Comment
Please, Sign In to add comment