Created
April 21, 2012 18:22
-
-
Save makaroni4/2438946 to your computer and use it in GitHub Desktop.
Caesar cypher for 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
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