Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
23 lines (20 sloc) 570 Bytes
class Caesar
def self.encipher(shift, message)
apply_algorithm(:encipher, shift, message)
end
def self.decipher(shift, message)
apply_algorithm(:decipher, shift, message)
end
private
def self.apply_algorithm(direction, shift, message)
letters = %w{A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
shift = direction == :encipher ? shift : -shift
message.upcase.split(//).map do |letter|
if letters.include?(letter)
letters.rotate(shift)[letters.index(letter)]
else
letter
end
end.join
end
end