Last active
October 15, 2015 20:03
-
-
Save thatrubylove/54cbba99ba8df3f505a1 to your computer and use it in GitHub Desktop.
ROT13 in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'byebug' | |
module Rot13 | |
extend self | |
LOWERS = ("a".."z").to_a | |
UPPERS = ("A".."Z").to_a | |
def cypher(val) | |
val.to_s.chars.map do |ch| | |
cypher_char(ch, LOWERS) || | |
cypher_char(ch, UPPERS) || | |
ch | |
end.join | |
end | |
alias_method :decypher, :cypher | |
private | |
def cypher_char(val, array) | |
return unless index = array.index(val) | |
array.reverse[index-13] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment