Class: DitDah::DitDah

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

Constant Summary collapse

MORSE_HASH =
Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDitDah

Returns a new instance of DitDah.



11
12
13
14
15
16
# File 'lib/dit_dah.rb', line 11

def initialize
  @input_text = nil
  @morse_code = Array.new

  init_morse_chars
end

Instance Attribute Details

#morseNalphaObject (readonly)

Returns the value of attribute morseNalpha.



7
8
9
# File 'lib/dit_dah.rb', line 7

def morseNalpha
  @morseNalpha
end

Instance Method Details

#get_input_textObject



22
23
24
# File 'lib/dit_dah.rb', line 22

def get_input_text
  @input_text
end

#get_morse_codeObject



42
43
44
# File 'lib/dit_dah.rb', line 42

def get_morse_code
  @morse_code
end

#init_morse_charsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dit_dah.rb', line 46

def init_morse_chars
  # key is the alphabet, value is the morse code
  MORSE_HASH['a'] = ".-"
  MORSE_HASH['b'] = "-..."
  MORSE_HASH['c'] = "-.-."
  MORSE_HASH['d'] = "-.."
  MORSE_HASH['e'] = "."
  MORSE_HASH['f'] = "..-."
  MORSE_HASH['g'] = "--."
  MORSE_HASH['h'] = "...."
  MORSE_HASH['i'] = ".."
  MORSE_HASH['j'] = ".---"
  MORSE_HASH['k'] = "-.-"
  MORSE_HASH['l'] = ".-.."
  MORSE_HASH['m'] = "--"
  MORSE_HASH['n'] = "-."
  MORSE_HASH['o'] = "---"
  MORSE_HASH['p'] = ".--."
  MORSE_HASH['q'] = "--.-"
  MORSE_HASH['r'] = ".-."
  MORSE_HASH['s'] = "..."
  MORSE_HASH['t'] = "-"
  MORSE_HASH['u'] = "..-"
  MORSE_HASH['v'] = "...-"
  MORSE_HASH['w'] = ".--"
  MORSE_HASH['x'] = "-..-"
  MORSE_HASH['y'] = "-.--"
  MORSE_HASH['z'] = "--.."
  MORSE_HASH['.'] = ".-.-.-"
  MORSE_HASH[','] = "--..--"
  MORSE_HASH['?'] = "..--.."
  MORSE_HASH['/'] = "-..-."
  MORSE_HASH['@'] = ".--.-."
  MORSE_HASH['1'] = ".----"
  MORSE_HASH['2'] = "..---"
  MORSE_HASH['3'] = "...--"
  MORSE_HASH['4'] = "....-"
  MORSE_HASH['5'] = "....."
  MORSE_HASH['6'] = "-...."
  MORSE_HASH['7'] = "--..."
  MORSE_HASH['8'] = "---.."
  MORSE_HASH['9'] = "----."
  MORSE_HASH['0'] = "-----"
  MORSE_HASH[' '] = "/" # the key is space character with the space as equivalent morse code
  MORSE_HASH[':'] = "---..."
  MORSE_HASH['-'] = "-....-"
  MORSE_HASH['('] = "-.--.-"
  MORSE_HASH[')'] = "-.--.-"
  MORSE_HASH['='] = "-...-"
end

#set_input_text(input_text) ⇒ Object



18
19
20
# File 'lib/dit_dah.rb', line 18

def set_input_text(input_text)
  @input_text = input_text.downcase.split("").map(&:to_s)
end

#to_morse_code(arg_text = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dit_dah.rb', line 26

def to_morse_code(arg_text=nil)
  text = arg_text.nil? ? get_input_text : arg_text.downcase.split("").map(&:to_s)

  @morse_code.clear # clear existing content before adding to array

  if !(text.nil?)
    text.each do |char|
      @morse_code.push(MORSE_HASH[char])
    end

    get_morse_code
  else
    raise NoInputError
  end
end