I was trying to refactor the following code for Caesar cipher, but it feels overly complicated at the end of the refactoring. Could someone else take a look at it and let me know if I am just overdoing/making it complicated for someone trying to read the code?
My highest priority is to write readable code, so any pointers in that regard would be welcome.
module CASE_WRAP
def cyclic_next!
if self == 'z'
replace 'a'
return
end
if self == 'Z'
replace 'A'
return
end
self.next!
end
def shift(number)
number.times { self.cyclic_next! }
self
end
end
class CaesarCipher
def self.cipher(message, shift)
CaesarCipher.new(shift: shift)
.encrypt message
end
def initialize(shift: 0)
@shift = shift
end
def encrypt(message)
message.gsub(/\w/) do |character|
character.extend(CASE_WRAP)
.shift @shift
end
end
end
Find tests for this in my repo.