Skip to content

Instantly share code, notes, and snippets.

@rwarbelow
Created March 11, 2014 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwarbelow/9490710 to your computer and use it in GitHub Desktop.
Save rwarbelow/9490710 to your computer and use it in GitHub Desktop.
We created a class called SecretCode that takes a word or sentence and encodes it. It also has a method that decodes the secret message.
ENCODE = { "a" => "j", "b" => "3", "c" => "l", "d" => "m", "e" => ".",
"f" => "o", "g" => "p", "h" => "4", "i" => "r", "j" => "&", "k" => "t",
"l" => "u", "m" => "v", "n" => "#", "o" => "x", "p" => "y", "q" => "9",
"r" => "a", "s" => "$", "t" => "c", "u" => "[", "v" => "e", "w" => "f",
"x" => "g", "y" => "8", "z" => "i", " " => " " }
DECODE = { "j" => "a", "3" => "b", "l" => "c", "m" => "d", "." => "e",
"o" => "f", "p" => "g", "4" => "h", "r" => "i", "&" => "j", "t" => "k",
"u" => "l", "v" => "m", "#" => "n", "x" => "o", "y" => "p", "9" => "q",
"r" => "a", "s" => "$", "t" => "c", "u" => "[", "v" => "e", "w" => "f",
"x" => "g", "y" => "8", "z" => "i" }
class SecretCode
def initialize(word)
@word = word
@encoded_word = []
@decoded_word = []
end
def split_word
@word.split("")
end
def encode
split_word.each do |letter|
@encoded_word << ENCODE[letter.downcase]
end
p @encoded_word.join
end
def decode
split_word.each do |letter|
@decoded_word << DECODE[letter.downcase]
end
p @decoded_word.join
end
end
code = SecretCode.new("Hello world")
code.encode
code = SecretCode.new("4.a. r$ j $.#c.#l.")
code.decode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment