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
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