Skip to content

Instantly share code, notes, and snippets.

@halilim
Last active August 30, 2017 11:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save halilim/7d94c7a960df72f9c5d9 to your computer and use it in GitHub Desktop.
CIPHER = { 'a' => 'n', 'b' => 'o', 'c' => 'p', 'd' => 'q',
'e' => 'r', 'f' => 's', 'g' => 't', 'h' => 'u',
'i' => 'v', 'j' => 'w', 'k' => 'x', 'l' => 'y',
'm' => 'z', 'n' => 'a', 'o' => 'b', 'p' => 'c',
'q' => 'd', 'r' => 'e', 's' => 'f', 't' => 'g',
'u' => 'h', 'v' => 'i', 'w' => 'j', 'x' => 'k',
'y' => 'l', 'z' => 'm' }.freeze
REVERSE_CIPHER = CIPHER.invert.freeze
def encrypt(string)
CIPHER.values_at(*string.downcase.chars).join
end
def decrypt(string)
REVERSE_CIPHER.values_at(*string.downcase.chars).join
end
# One liner edition for DRY OCD :)
%i(en de).each do |prefix|
define_method "#{prefix}crypt2" do |string|
(prefix == :en ? CIPHER : REVERSE_CIPHER).values_at(*string.downcase.chars).join
end
end
require 'minitest/autorun'
class TestRubyCryptology < Minitest::Test
def test_encrypt
assert_equal 'grfg', encrypt('test')
end
def test_decrypt
assert_equal 'test', decrypt('grfg')
end
def test_encrypt2
assert_equal 'grfg', encrypt2('test')
end
def test_decrypt2
assert_equal 'test', decrypt2('grfg')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment