Class: MorseCode

Inherits:
Object
  • Object
show all
Defined in:
lib/morsecode.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_string = '') ⇒ MorseCode

Returns a new instance of MorseCode.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/morsecode.rb', line 8

def initialize(input_string='')
  
  @h = {
    "t"=>"1", "m"=>"11", "n"=>"12", "o"=>"111", "g"=>"112", "q"=>"1121", 
    "z"=>"1122", "k"=>"121", "d"=>"122", "-"=>"2211", "."=>"212121", 
    "y"=>"1211", "c"=>"1212", "x"=>"1221", "b"=>"1222", "e"=>"2", "a"=>"21",
    "i"=>"22", "w"=>"211", "r"=>"212", ""=>"2121", "l"=>"2122", "u"=>"221", 
    "s"=>"222", "j"=>"2111", "p"=>"2112", "f"=>"2212", "v"=>"2221", 
    "h"=>"2222", "1"=>"21111", "2"=>"22111", "3"=>"22211", "4"=>"22221", 
    "5"=>"22222", "6"=>"12222", "7"=>"11222", "8"=>"11122", "9"=>"11112",
    "0"=>"11111", " "=>"5", "_"=>"221121", ":"=>"222111", "/"=>"12212", 
    "@"=>"211212", "?"=>"221122", "^"=>"3"
  }
  
  @input_string = input_string
end

Instance Method Details

#to_mcObject



30
31
32
# File 'lib/morsecode.rb', line 30

def to_mc
  @input_string.split(//).map{|char| @h[char]}.join('4')
end

#to_sObject



25
26
27
28
# File 'lib/morsecode.rb', line 25

def to_s
  @input_string.split(/5/).map \
      {|word| word.split(/4/).map{|code| @h.invert[code]}.join }.join ' '
end