morse 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +17 -0
- data/lib/morse.rb +69 -0
- data/tests/test_morse.rb +15 -0
- metadata +47 -0
data/README
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Ben's Morse Code Module
|
2
|
+
-----------------------
|
3
|
+
|
4
|
+
ben@bensinclair.com
|
5
|
+
http://www.BenSinclair.com
|
6
|
+
|
7
|
+
This software is released into the public domain with no warranty.
|
8
|
+
|
9
|
+
Usage:
|
10
|
+
|
11
|
+
require 'morse'
|
12
|
+
|
13
|
+
Morse.encode 'hello world' => ".... . .-.. .-.. --- .-- --- .-. .-.. -.."
|
14
|
+
|
15
|
+
Morse.decode '... --- ...' => "HELLO WORLD"
|
16
|
+
|
17
|
+
|
data/lib/morse.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Ben's Morse Code Module
|
2
|
+
# http://www.BenSinclair.com/articles/ruby-morse-code-module
|
3
|
+
|
4
|
+
module Morse
|
5
|
+
|
6
|
+
TO_MORSE_DATA = Hash.new
|
7
|
+
TO_MORSE_DATA['A']='.-'
|
8
|
+
TO_MORSE_DATA['B']='-...'
|
9
|
+
TO_MORSE_DATA['C']='-.-.'
|
10
|
+
TO_MORSE_DATA['D']='-..'
|
11
|
+
TO_MORSE_DATA['E']='.'
|
12
|
+
TO_MORSE_DATA['F']='..-.'
|
13
|
+
TO_MORSE_DATA['G']='--.'
|
14
|
+
TO_MORSE_DATA['H']='....'
|
15
|
+
TO_MORSE_DATA['I']='..'
|
16
|
+
TO_MORSE_DATA['J']='.---'
|
17
|
+
TO_MORSE_DATA['K']='-.-'
|
18
|
+
TO_MORSE_DATA['L']='.-..'
|
19
|
+
TO_MORSE_DATA['M']='--'
|
20
|
+
TO_MORSE_DATA['N']='-.'
|
21
|
+
TO_MORSE_DATA['O']='---'
|
22
|
+
TO_MORSE_DATA['P']='.--.'
|
23
|
+
TO_MORSE_DATA['Q']='--.-'
|
24
|
+
TO_MORSE_DATA['R']='.-.'
|
25
|
+
TO_MORSE_DATA['S']='...'
|
26
|
+
TO_MORSE_DATA['T']='-'
|
27
|
+
TO_MORSE_DATA['U']='..-'
|
28
|
+
TO_MORSE_DATA['V']='...-'
|
29
|
+
TO_MORSE_DATA['W']='.--'
|
30
|
+
TO_MORSE_DATA['X']='-..-'
|
31
|
+
TO_MORSE_DATA['Y']='-.--'
|
32
|
+
TO_MORSE_DATA['Z']='--..'
|
33
|
+
TO_MORSE_DATA['0']='-----'
|
34
|
+
TO_MORSE_DATA['1']='.----'
|
35
|
+
TO_MORSE_DATA['2']='..---'
|
36
|
+
TO_MORSE_DATA['3']='...--'
|
37
|
+
TO_MORSE_DATA['4']='....-'
|
38
|
+
TO_MORSE_DATA['5']='.....'
|
39
|
+
TO_MORSE_DATA['6']='-....'
|
40
|
+
TO_MORSE_DATA['7']='--...'
|
41
|
+
TO_MORSE_DATA['8']='---..'
|
42
|
+
TO_MORSE_DATA['9']='----.'
|
43
|
+
FROM_MORSE_DATA = TO_MORSE_DATA.invert
|
44
|
+
|
45
|
+
# Encodes text to morse code.
|
46
|
+
def Morse.encode(text)
|
47
|
+
result = ""
|
48
|
+
text.upcase.split('').each {|letter|
|
49
|
+
result << ' ' if letter == ' '
|
50
|
+
result << TO_MORSE_DATA[letter] << ' ' unless letter == ' ' or TO_MORSE_DATA[letter] == nil
|
51
|
+
}
|
52
|
+
result.chop
|
53
|
+
end
|
54
|
+
|
55
|
+
# Decodes morse code to text
|
56
|
+
def Morse.decode(morse)
|
57
|
+
result = ""
|
58
|
+
morse.upcase.split(' ').each {|word|
|
59
|
+
word.split(' ').each {|code|
|
60
|
+
result << '?' if FROM_MORSE_DATA[code] == nil
|
61
|
+
result << FROM_MORSE_DATA[code] unless FROM_MORSE_DATA[code] == nil
|
62
|
+
}
|
63
|
+
result << ' '
|
64
|
+
}
|
65
|
+
result.chop
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
data/tests/test_morse.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'morse'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestMorse < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_encode
|
7
|
+
assert_equal('.... . .-.. .-.. --- .-- --- .-. .-.. -..', Morse.encode('hello world') )
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_decode
|
11
|
+
assert_equal('HELLO WORLD', Morse.decode('.... . .-.. .-.. --- .-- --- .-. .-.. -..'))
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: morse
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-01-31 00:00:00 -06:00
|
8
|
+
summary: Converts text to and from morse code
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ben@bensinclair.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: morse
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Ben Sinclair
|
31
|
+
files:
|
32
|
+
- lib/morse.rb
|
33
|
+
- README
|
34
|
+
test_files:
|
35
|
+
- tests/test_morse.rb
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
requirements: []
|
45
|
+
|
46
|
+
dependencies: []
|
47
|
+
|