-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample2.rb
45 lines (41 loc) · 1.09 KB
/
sample2.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
# Here's a different way to add up an array.
fred = [ 4, 19, 3, 7, 32 ]
sum = 0
fred.each { |i| sum += i }
print "Sum of [", fred.join(" "), "] is #{sum}\n"
# Or create a secret message.
key = { 'A' => 'U', 'B' => 'Q', 'C' => 'A', 'D' => 'F', 'E' => 'D', 'F' => 'K',
'G' => 'P', 'H' => 'W', 'I' => 'N', 'J' => 'L', 'K' => 'J', 'L' => 'M',
'M' => 'S', 'N' => 'V', 'O' => 'Y', 'P' => 'O', 'Q' => 'Z', 'R' => 'T',
'S' => 'E', 'T' => 'I', 'U' => 'X', 'V' => 'B', 'W' => 'G', 'X' => 'H',
'Y' => 'R', 'Z' => 'C' }
print "\nThe encoded message is: "
"The secret message".each_byte do | b |
b = b.chr.upcase
if key.has_key?(b) then
print key[b]
else
print b
end
end
print "\n"
# But give us the info to read it anyway.
print "The key is: "
ct = 8
key.each { | k, v |
if ct == 8 then
print "\n "
ct = 0
else
print ", "
end
ct = ct + 1
print "#{v} => #{k}"
}
print "\n\n"
# Some interesting things from Integer.
3.times { print "Hi! " }
print "\n"
print "Count: "
3.upto(7) { |n| print n, " " }
print "\n"