Sha256: ea240847172afe79fb5a2243913f6eb0ef33da5fdb22a9fdc95848e28889fcad

Contents?: true

Size: 1.22 KB

Versions: 26

Compression:

Stored size: 1.22 KB

Contents

#!/usr/bin/env ruby

class Morse

  MAPPING = {
	'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'  => '--..',
	'0'  => '-----',
	'1'  => '.----',
	'2'  => '..---',
	'3'  => '...--',
	'4'  => '....-',
	'5'  => '.....',
	'6'  => '-....',
	'7'  => '--...',
	'8'  => '---..',
	'9'  => '----.',
	' '  => ' / ',
  }

  def self.text_to_morse(string)
    result = ''
    string.scan(/[a-zA-Z0-9]| /).each do |word|
      word.each_char do |char|
        result << MAPPING[char] + ' '
      end
      result << ' / '
    end
    result
  end

  def self.morse_to_text(string)
    result = ''
    string.split(' / ').each do |word|
      word.split(' ').each do |char|
        MAPPING.each do |k, v|
          if v == char
            result << k
          end
        end
      end
      result << ' '
    end
    result
  end

end

STDIN.each_line do |line|
  puts Morse.morse_to_text(line)
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ix-cli-0.0.27 bin/ix-morse-to-text
ix-cli-0.0.26 bin/ix-morse-to-text
ix-cli-0.0.25 bin/ix-morse-to-text
ix-cli-0.0.24 bin/ix-morse-to-text
ix-cli-0.0.23 bin/ix-morse-to-text
ix-cli-0.0.22 bin/ix-morse-to-text
ix-cli-0.0.21 bin/ix-morse-to-text
ix-cli-0.0.20 bin/ix-morse-to-text
ix-cli-0.0.19 bin/ix-morse-to-text
ix-cli-0.0.18 bin/ix-morse-to-text
ix-cli-0.0.17 bin/ix-morse-to-text
ix-cli-0.0.16 bin/ix-morse-to-text
ix-cli-0.0.15 bin/ix-morse-to-text
ix-cli-0.0.14 bin/ix-morse-to-text
ix-cli-0.0.13 bin/ix-morse-to-text
ix-cli-0.0.12 bin/ix-morse-to-text
ix-cli-0.0.11 bin/ix-morse-to-text
ix-cli-0.0.10 bin/ix-morse-to-text
ix-cli-0.0.9 bin/ix-morse-to-text
ix-cli-0.0.7 bin/ix-morse-to-text