/
names.rb
55 lines (52 loc) · 1.99 KB
/
names.rb
1
2
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
45
46
47
48
49
50
51
52
53
54
55
module CompSci
module Names
ENGLISH_UPPER = [*'A'..'Z']
ENGLISH_LOWER = [*'a'..'z']
WW1 = [:apples, :butter, :charlie, :duff, :edward, :freddy, :george,
:harry, :ink, :johnnie, :king, :london, :monkey, :nuts, :orange,
:pudding, :queenie, :robert, :sugar, :tommy, :uncle, :vinegar,
:willie, :xerxes, :yellow, :zebra]
WW2 = [:able, :baker, :charlie, :dog, :easy, :fox, :george, :how, :item,
:jig, :king, :love, :mike, :nan, :oboe, :peter, :queen, :roger,
:sugar, :tare, :uncle, :victor, :william, :xray, :yoke, :zebra]
NATO = [:alfa, :bravo, :charlie, :delta, :echo, :foxtrot, :golf, :hotel,
:india, :juliett, :kilo, :lima, :mike, :november, :oscar, :papa,
:quebec, :romeo, :sierra, :tango, :uniform, :victor, :whiskey,
:xray, :yankee, :zulu]
CRYPTO = [:alice, :bob, :charlie, :david, :eve, :frank, :grace, :heidi,
:judy, :mallory, :olivia, :peggy, :sybil, :trudy, :victor,
:wendy]
PLANETS = [:mercury, :venus, :earth, :mars, :jupiter, :saturn, :uranus,
:neptune, :pluto]
SOLAR = [:mercury, :venus, :earth, :mars, :asteroid_belt, :jupiter,
:saturn, :uranus, :neptune, :kuiper_belt, :scattered_disk,
:heliosphere]
# map val to [0..names.length]
def self.assign(val, names)
case val
when String
if val.match %r{\A\d+\z}
pos = val.to_i
else
case names.size
when (1..26)
pos = val[0].upcase.ord - 'A'.ord
when (27..99)
pos = val[0].ord - 'A'.ord
pos -= 6 if pos > 26
else
raise "unexpected names.size: #{names.size}"
end
end
when Integer
pos = val
else
raise "unexpected val: #{val} (#{val.class})"
end
if pos < 0 or pos >= names.size
raise "val #{val} pos #{pos} outside of names range (#{names.size})"
end
names[pos]
end
end
end