Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now a gem

  • Loading branch information...
commit 1a03adfa89d1cccd21e6ffcfb7976095c2423323 1 parent a892bdd
@EricR authored
View
BIN  Caesar.rb-1.0.1.gem
Binary file not shown
View
17 caesar.gemspec
@@ -0,0 +1,17 @@
+lib = File.expand_path('../lib/', __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require 'bundler/version'
+
+Gem::Specification.new do |s|
+ s.name = "Caesar.rb"
+ s.version = "1.0.1"
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Eric Rafaloff"]
+ s.email = ["hello@ericrafaloff.com"]
+ s.homepage = "http://github.com/ericr/caesar.rb"
+ s.summary = "A simple implementation of the Caesar Cipher in 22 lines of Ruby."
+ s.description = "A simple implementation of the Caesar Cipher in 22 lines of Ruby."
+ s.files = Dir.glob("{lib}/**/*") + %w(README.markdown)
+ s.require_path = 'lib'
+end
View
22 lib/caesar.rb
@@ -0,0 +1,22 @@
+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
Please sign in to comment.
Something went wrong with that request. Please try again.