Class: MorseCode::Decoder

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

Instance Method Summary collapse

Methods included from Cacheable

#cache, #fetch

Constructor Details

#initialize(message = '') ⇒ Decoder

Returns a new instance of Decoder.



12
13
14
# File 'lib/morse_code/decoder.rb', line 12

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

Instance Method Details

#decodeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/morse_code/decoder.rb', line 16

def decode
  [].tap do |decode_words|
    word = []
    @message.split(/\s+/).each do |char|
      if char == '/'
        decode_words.push(word.join)
        word = []
      else
        word.push(decode_char(char))
      end
    end
    decode_words.push(word.join) unless word.empty?
  end.join(' ')
end

#decode_withObject Also known as: dit_dah_to



31
32
33
34
# File 'lib/morse_code/decoder.rb', line 31

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