-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmorse_code.rb
More file actions
69 lines (63 loc) · 1.79 KB
/
morse_code.rb
File metadata and controls
69 lines (63 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
HASH_MORSE = {
'.-' => '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',
'----.' => '8',
'-----' => '0',
'/' => ' ' # Mapping foward bar to be a space
}
class ConvertFromMorse
def get_translated(string_morse)
@translated_string = ""
puts translate(string_morse)
end
def mk_morse_code line
line.split('').map{|c| HASH_MORSE.index c.downcase}.join(' ')
end
private
def translate(string_morse)
array_morse = string_morse.split()
array_morse.each{|morse| @translated_string += HASH_MORSE[morse] unless HASH_MORSE[morse].nil? }
@translated_string
end
end
morse = ConvertFromMorse.new
morse.get_translated("-- --- .-. ... . / -.-. --- -.. . / - .... . / -. . .-- / -.. . .- -.. / .-.. .- -. --. ..- .- --. . .-.-.-")
morse.get_translated("--- -- --. .. ... -- --- .-. ... . -.-. --- -..")
morse.get_translated("- .-- . . - ... / .- .-. . / . ...- . -. / ... .... --- .-. - . .-. / .-- .. - .... / -- --- .-. ... . / -.-. --- -..")
morse.get_translated("-- --- .-. ... . -.-. --- -.. . .. ... -... .- -.. ..-. --- .-. - .-- . . - .. -. --")
morse.get_translated(".. - / .. ... / .- / .--. .-.. . .- ... ..- .-. . / - --- / -- . . - / -.-- --- ..- / .- ... / .-- . .-.. .-.")
mc = morse.mk_morse_code('HAHAHA Interesting! Thx PotHix.')
puts mc
morse.get_translated mc