Class: MorseCode::Converter
- Inherits:
-
Object
- Object
- MorseCode::Converter
- Defined in:
- lib/morse_code/converter.rb
Instance Method Summary collapse
- #clean_text ⇒ Object
- #convert ⇒ Object
- #convert_line(line) ⇒ Object
-
#initialize(text) ⇒ Converter
constructor
A new instance of Converter.
- #valid? ⇒ Boolean
Constructor Details
#initialize(text) ⇒ Converter
4 5 6 |
# File 'lib/morse_code/converter.rb', line 4 def initialize(text) @text = text end |
Instance Method Details
#clean_text ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/morse_code/converter.rb', line 8 def clean_text cleaned_text = "" @text.split(" ").each do |potential_morse_code| latin_character = MorseCode::Constants::FULL_CODE[potential_morse_code] cleaned_text << [potential_morse_code, " "].join if latin_character end cleaned_text.strip end |
#convert ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/morse_code/converter.rb', line 24 def convert translated_text = "" if @text.lines.count > 1 @text.each_line do |line| translated_text << convert_line(line) translated_text << "\n" end else translated_text << convert_line(@text) end translated_text end |
#convert_line(line) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/morse_code/converter.rb', line 38 def convert_line(line) converted_text = "" line.strip.split(" ").each do |potential_morse_code| latin_character = MorseCode::Constants::FULL_CODE[potential_morse_code] converted_text << latin_character if latin_character end converted_text end |
#valid? ⇒ Boolean
17 18 19 20 21 22 |
# File 'lib/morse_code/converter.rb', line 17 def valid? @text.split(" ").each do |potential_morse_code| return false unless MorseCode::Constants::FULL_CODE[potential_morse_code] end true end |