Class: MorseCode::Decoder

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

Instance Method Summary collapse

Constructor Details

#initialize(message = '') ⇒ Decoder

Returns a new instance of Decoder.



5
6
7
# File 'lib/morse_code/decoder.rb', line 5

def initialize(message = '')
  @message = message.dup
end

Instance Method Details

#decodeObject



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

def decode
  [].tap do |decode_words|
    word = []
    @message.split(/\s+/).each do |char|
      if char == '/'
        decode_words.push(word.join)
        word = []
      else
        word.push(MorseCode::DECODE_MAP[char] || char)
      end
    end
    decode_words.push(word.join) if word.size > 0
  end.join(' ')
end

#decode_withObject Also known as: dit_dah_to



24
25
26
27
# File 'lib/morse_code/decoder.rb', line 24

def decode_with
  @message.tap { |message| message.gsub!('DIT', '.'); message.gsub!('DAH', '-') }
  decode
end