Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created April 21, 2012 18:22
Show Gist options
  • Save makaroni4/2438946 to your computer and use it in GitHub Desktop.
Save makaroni4/2438946 to your computer and use it in GitHub Desktop.
Caesar cypher for Ruby
class String
def to_caesar shift
shift = (shift % 26).abs
each_byte.map { |b| ((b + shift - 97) % 26 + 97).chr }.join
end
end
a = "Caesar was a cool guy"
p a.split.map { |w| w.to_caesar 4 }.join(' ') # => "aeiwev aew e gssp kyc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment