Last active
April 26, 2016 23:53
-
-
Save XanClic/fcc796f694e98b31f678957b8b90303d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
MORSE_DICT = { | |
'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' => '--..', | |
' ' => ' ', | |
'1' => '.----', | |
'2' => '..---', | |
'3' => '...--', | |
'4' => '....-', | |
'5' => '.....', | |
'6' => '-....', | |
'7' => '--...', | |
'8' => '---..', | |
'9' => '----.', | |
'0' => '-----', | |
'à' => '.--.-', | |
'ä' => '.-.-', | |
'è' => '.-..-', | |
'é' => '..-..', | |
'ö' => '---.', | |
'ü' => '..--', | |
'ß' => '...--..', | |
'ñ' => '--.--', | |
'.' => '.-.-.-', | |
',' => '--..--', | |
':' => '---...', | |
';' => '-.-.-.', | |
'?' => '..--..', | |
'!' => '-.-.--', | |
'-' => '-....-', | |
'_' => '..--.-', | |
'(' => '-.--.', | |
')' => '-.--.-', | |
"'" => '.----.', | |
'=' => '-...-', | |
'+' => '.-.-.', | |
'/' => '-..-.', | |
'@' => '.--.-.', | |
'$' => '...-..-', | |
"\x2" => '-.-.-', # Starting signal: STX | |
"\x4" => '...-.-', # End of work: EOT | |
"\b" => '........', # Error: BS | |
"\x5" => '.-...', # Wait: ENQ | |
"\x6" => '...-.', # Understood: ACK | |
"\x7" => '...---...', # SOS: BEL | |
} | |
UNMORSE_DICT = MORSE_DICT.invert.merge { | |
'*' => ' ', | |
'----' => 'ch' | |
} | |
def to_morse(str) | |
str.chars.map do |c| | |
MORSE_DICT[c] or $stderr.puts("Unknown character '#{c}'") | |
end * ' ' | |
end | |
def from_morse(str) | |
str.gsub(/ +/, ' * ').split.map do |m| | |
UNMORSE_DICT[m] or $stderr.puts("Unknown sequence '#{m}'") | |
end.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment