Class: MorseCode::Decoder
- Inherits:
-
Object
- Object
- MorseCode::Decoder
- Includes:
- Cacheable
- Defined in:
- lib/morse_code/decoder.rb
Instance Method Summary collapse
- #decode ⇒ Object
- #decode_with ⇒ Object (also: #dit_dah_to)
-
#initialize(message = '') ⇒ Decoder
constructor
A new instance of Decoder.
Methods included from Cacheable
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 = .dup end |
Instance Method Details
#decode ⇒ Object
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_with ⇒ Object Also known as: dit_dah_to
31 32 33 34 |
# File 'lib/morse_code/decoder.rb', line 31 def decode_with @message.tap { || .gsub!('DIT', '.'); .gsub!('DAH', '-') } decode end |