Alex Alex - 6 months ago 30
Ruby Question

Convert string through hash

I'm trying to convert a string of morse code into words. I split the string into words. Then I split each word into a secondary array of letters and numbers.

def self.decode(str)
decoded = ""
arr = []
p arr = str.split(' ')
p arr.map! { |i| i.split(' ') }

arr.each do |r|
r.each do |i|
decoded += @morse.invert[i].to_s[-1]
end
end

p decoded
end


In my hash, I use numbers. They start with
N
. In
to_s[-1]
, the
-1
is to drop the
N
.

I get this error:

`+': no implicit conversion of nil into String


I can figure out how to get past it though because I don't see a
nil
value in the array.

class Morse

@morse = {
A: '.-',
B: '-...',
C: '-.-.',
D: '-..',
E: '.',
F: '..-.',
G: '--.',
H: '....',
I: '..',
J: '.---',
K: '-.-',
L: '.-..',
M: '--',
N: '-.',
O: '---',
P: '.--.',
Q: '--.-',
R: '.-.',
S: '...',
T: '-',
U: '..-',
V: '...-',
W: '.--',
X: '-..-',
Y: '-.--',
Z: '--..',
' ': ' ' ,
N1: '.----',
N2: '..---',
N3: '...--',
N4: '....-',
N5: '.....',
N6: '-....',
N7: '--...',
N8: '---..',
N9: '----.',
N0: '-----'
}

def self.encode(str)
encoded = ""
sym_temp = ""
str = str.upcase!
str.each_char do |c|
ascii_check = c.ord
if ascii_check.between?(65,90)
temp = str[c].to_sym
encoded += "#{@morse[temp]}" + " "
elsif ascii_check.between?(48,57)
temp = "N".concat(str[c]).to_sym
encoded += "#{@morse[temp]}" + " "
elsif ascii_check ===(32)
temp = str[c].to_sym
encoded += "#{@morse[temp]}"
end
end
p encoded
end

def self.decode(str)
decoded = ""
arr = []
# str.split(' ').map do |i|
p arr = str.split(' ')
p arr.map! { |i| i.split(' ') }

arr.each do |r|
r.each do |i|
p decoded += @morse.invert[i].to_s[-1].to_s
end
end

p decoded
end

# def self.read_file
# # @Temp = File.read("preamble_encode.txt").chomp
# # File.open('hiscore.txt'){|f| f.lines.map{|l| l.chomp!.split(',')}}
# # @Temp = File.foreach("preamble_encode.txt", 'r') {|f| f.lines.map{|l| l.chomp}}

# end

# def self.write_file
# # Temp2 = File.open('preamble_decode.txt', 'w') do |f|
# # f.puts Temp2
# # end
# end

end


test = "Abc 1oL!"
test2 = ".-- . - .... . .--. . --- .--. .-.. ."
Morse.encode(test)
Morse.decode(test2)

Answer

Your problem is here:

        :P => :'.--.',

Your colon on the right hand side of the hash rocket is making that a symbol, not a string. As it happens, your test case for decode includes a P. When you try to look up the String '.--.', it doesn't find it and returns nil.

Comments