stuartambient stuartambient - 3 years ago 51
Ruby Question

Trying to concat 2 arrays that are output from the map method

I'm doing a 'morse code' exercise and running into some difficulty. I'll skip posting the hash I created that stores the code and the letter.

The morse code in the method call has 3 spaces between the 'words' Example -

decodeMorse('.... . -.-- .--- ..- -.. .')


My strategy was to split the words first using
split(/\s\s\s/)
which gives me separate arrays for each word, but then those arrays need a
split(' ')
to get to the letters.

This is my code -

sc = str.split(/\s\s\s/)
sc.each do |string|
string.split(' ').map {|key| morsecode[key]; }


It works okay, but I'm left with two arrays at the end:

=> ["h", "e", "y"]
=> ["j", "u", "d", "e"]


Normally, if I had two or more arrays that had assigned variable names I would know how to concat them but what I've tried and searched on hasn't changed the situation. Obviously all I get from join('') is the two words together with no space between them.

Answer Source
  sc = str.split(/\s\s\s/)

  deciphered = sc.map do |string|
    string.split(' ').map {|key| morsecode[key]; }.join
  end

  deciphered.join(' ')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download