Class: MorseCode::Encoder

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

Instance Method Summary collapse

Methods included from Cacheable

#cache, #fetch

Constructor Details

#initialize(message = '') ⇒ Encoder

Returns a new instance of Encoder.



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

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

Instance Method Details

#encodeObject



16
17
18
19
20
21
22
23
# File 'lib/morse_code/encoder.rb', line 16

def encode
  @encode ||= [].tap do |encode_words|
    @message.split(/\s+/).each do |word|
      encode_words.concat(encode_word(word))
    end
    encode_words.pop
  end.join(' ')
end

#encode_withObject Also known as: dit_dah



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

def encode_with
  encode.dup.tap { |message| message.gsub!('.', 'DIT').gsub!('-', 'DAH') }
end

#waveObject



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

def wave
  MorseCode::Wave.new(encode).generate
end