Class: Telegraph::MorseTransmission

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

Overview

Telegraph::MorseTransmission class provides a simple text-to-morse, morse-to-text translator using user defined characters for short and long signals (defaults to ‘.’ and ‘-’).

It uses as reference the document <t>RECOMMENDATION ITU-R M.1677</t> from the International Telecommunication Union, Radiocommunication Sector (ITU-R), the United Nations agency for information and communication technology issues.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signals = {}) ⇒ MorseTransmission

When instantiating the class two options can be provided to set the signals to use by the new instance when reading and writing morse code:

  • :short - The character to use as dot or short signal. Default: ‘.’

  • :long - The character to use as dash or long signal. Default: ‘-’



58
59
60
61
# File 'lib/telegraph.rb', line 58

def initialize(signals={})
  @short = signals[:short] || Telegraph::DOT
  @long = signals[:long] || Telegraph::DASH
end

Instance Attribute Details

#longObject

Returns the value of attribute long.



50
51
52
# File 'lib/telegraph.rb', line 50

def long
  @long
end

#shortObject

Returns the value of attribute short.



50
51
52
# File 'lib/telegraph.rb', line 50

def short
  @short
end

Instance Method Details

#end_of_workObject

Returns the ‘End of work’ sign using the signal characters defined for this instance.



112
113
114
# File 'lib/telegraph.rb', line 112

def end_of_work
  dot_dash(Telegraph::End_of_work)
end

#errorObject

Returns the ‘Error’ sign using the signal characters defined for this instance.



92
93
94
# File 'lib/telegraph.rb', line 92

def error
  dot_dash(Telegraph::Error)
end

#invitation_to_transmitObject

Returns the ‘Invitation to transmit’ sign using the signal characters defined for this instance.



102
103
104
# File 'lib/telegraph.rb', line 102

def invitation_to_transmit
  dot_dash(Telegraph::Invitation_to_transmit)
end

#morse_to_text(morse) ⇒ Object

Transforms a string of morse code into a string of text decoding using the RECOMMENDATION ITU-R M.1677.

It expects as short and long signals the characters defined by the user when the class was instantiated, a space as separation between letters and seven spaces as separation between words.

The returned string is completly downcased.

morser = Telegraph::MorseTransmission.new(:short =>'x', :long => '3')
morser.morse_to_text("xxxx x x3xx x3xx 333        x33 333 x3x x3xx 3xx")  #=> hello world"


86
87
88
89
# File 'lib/telegraph.rb', line 86

def morse_to_text(morse)
  morse = undo_dot_dash(morse.strip)
  Telegraph.morse_to_text(morse)
end

#starting_signalObject

Returns the ‘Starting signal’ sign using the signal characters defined for this instance.



117
118
119
# File 'lib/telegraph.rb', line 117

def starting_signal
  dot_dash(Telegraph::Starting_signal)
end

#text_to_morse(text) ⇒ Object

Transforms a string of plain text into a string of morse code using as short and long signals the characters defined by the user when the class was instantiated.

The returned string uses a single space as letter separator and a seven spaces gap as word separator.

morser = Telegraph::MorseTransmission.new(:short =>'x', :long => '3')
morser.text_to_morse("Hello world")  #=> "xxxx x x3xx x3xx 333        x33 333 x3x x3xx 3xx"


71
72
73
74
# File 'lib/telegraph.rb', line 71

def text_to_morse(text)
  morse = Telegraph.text_to_morse(text)
  dot_dash(morse)
end

#understoodObject

Returns the ‘Understood’ sign using the signal characters defined for this instance.



97
98
99
# File 'lib/telegraph.rb', line 97

def understood
  dot_dash(Telegraph::Understood)
end

#waitObject

Returns the ‘Wait’ sign using the signal characters defined for this instance.



107
108
109
# File 'lib/telegraph.rb', line 107

def wait
  dot_dash(Telegraph::Wait)
end