Class: MorseCode

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

Constant Summary collapse

@@map =
nil

Instance Method Summary collapse

Constructor Details

#initializeMorseCode

Returns a new instance of MorseCode.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/morsecode.rb', line 3

def initialize 
  if @@map == nil then
    @@map = Hash.new
    @@map["a"] = ". _"
           @@map["b"]  = "_ . . ."
           @@map["c"]  = "_ . _ ."
           @@map["d"] =  "_ . ."
           @@map["e"] =  "."
           @@map["f"] =  ". . _ ."
    @@map["g"] =  "_ _ ."
    @@map["h"] =  ". . . ."
    @@map["i"] =  ". ."
    @@map["j"] =  ". _ _ _"
    @@map["k"] =  "_ . _"
    @@map["l"] =  ". _ . ."
    @@map["m"] =  "_ _"
    @@map["n"] =  "_ ."
    @@map["o"] =  "_ _ _"
    @@map["p"] =  ". _ _ ."
    @@map["q"] =  "_ _ . _"
    @@map["r"] =  ". _ ."
    @@map["s"] =  ". . ."
    @@map["t"] =  "_"
    @@map["u"] =  ". . _"
    @@map["v"] =  ". . . _"
    @@map["w"] =  ". _ _"
    @@map["x"] =  "_ . . _"
    @@map["y"] =  "_ . _ _"
    @@map["z"] =  "_ _ . ."
    @@map["0"] =  "_ _ _ _ _"
    @@map["1"] =  ". _ _ _ _"
    @@map["2"] =  ". . _ _ _"
    @@map["3"] =  ". . . _ _"
    @@map["4"] =  ". . . . _"
    @@map["5"] =  ". . . . ."
    @@map["6"] =  "_ . . . ."
    @@map["7"] =  "_ _ . . ."
    @@map["8"] =  "_ _ _ . ."
    @@map["9"] =  "_ _ _ _ ."
    @@map[" "] = "+"
  end
end

Instance Method Details

#convert(text) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/morsecode.rb', line 45

def convert text
  unless text.match /[a-zA-Z0-9 ]/
    "Please use alpha-numeric characters right now"
  else
    output = ""
    text.split("").each do |char|
      output = output + @@map[char]
    end
    output
  end

end