Permalink
Browse files

Now a gem

  • Loading branch information...
1 parent a892bdd commit 1a03adfa89d1cccd21e6ffcfb7976095c2423323 @EricR committed Dec 30, 2011
Showing with 39 additions and 0 deletions.
  1. BIN Caesar.rb-1.0.1.gem
  2. +17 −0 caesar.gemspec
  3. +22 −0 lib/caesar.rb
View
Binary file not shown.
View
@@ -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
@@ -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

0 comments on commit 1a03adf

Please sign in to comment.