Skip to content

Instantly share code, notes, and snippets.

@thatrubylove
Last active October 15, 2015 20:03
Show Gist options
  • Save thatrubylove/54cbba99ba8df3f505a1 to your computer and use it in GitHub Desktop.
Save thatrubylove/54cbba99ba8df3f505a1 to your computer and use it in GitHub Desktop.
ROT13 in Ruby
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