rubylabs 0.9.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/bitlab.rb +22 -3
- data/lib/hashlab.rb +8 -8
- data/lib/randomlab.rb +22 -34
- data/lib/rubylabs.rb +19 -28
- data/lib/spherelab.rb +2 -2
- data/lib/tsplab.rb +12 -0
- data/test/hash_test.rb +1 -0
- data/test/rubylabs_test.rb +7 -7
- metadata +37 -58
- data/lib/encryptionlab.rb +0 -42
- data/test/encryption_test.rb +0 -23
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a37f158acc1a78c47cc98c2e28fdfdd039041aa
|
4
|
+
data.tar.gz: 5cacbad6cc3c4cbbf0a8c8b0055ccbd8b8240fab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 16ebf839dd3bd5ebf8823265cf9b48b13a75798d69dcc127259dc2660da7330b529737026aaa3d6d8a504442d5527a2382388f48496a1186ffa0c92e4e35a1d7
|
7
|
+
data.tar.gz: 108bbb297f34f25519863f534f44ea880bfcad617318250a0949c6cfdca478e35ee28629e8487b9eb4d620fa87ddfd5dbc23714f6e14e3e9443030b8e57df128
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/lib/bitlab.rb
CHANGED
@@ -174,10 +174,8 @@ module BitLab
|
|
174
174
|
m.array.each do |x|
|
175
175
|
if x.even_parity?
|
176
176
|
res << (x.value >> 1).chr
|
177
|
-
elsif $KCODE[0] == ?U
|
178
|
-
res << "\xe2\x80\xa2"
|
179
177
|
else
|
180
|
-
res << "
|
178
|
+
res << "\xe2\x80\xa2"
|
181
179
|
end
|
182
180
|
end
|
183
181
|
else
|
@@ -1042,3 +1040,24 @@ characters in +String+ objects.
|
|
1042
1040
|
end
|
1043
1041
|
|
1044
1042
|
end
|
1043
|
+
|
1044
|
+
class String
|
1045
|
+
|
1046
|
+
=begin rdoc
|
1047
|
+
|
1048
|
+
== String
|
1049
|
+
|
1050
|
+
In Ruby 1.9 and above elements in strings are chars, not integers. This
|
1051
|
+
method adds a code method to the String class that returns a Code object
|
1052
|
+
for a single-letter string.
|
1053
|
+
=end
|
1054
|
+
|
1055
|
+
def code(*args)
|
1056
|
+
if self.length > 1
|
1057
|
+
raise "code: defined only for single characters or integers"
|
1058
|
+
else
|
1059
|
+
return self[0].ord.code(*args)
|
1060
|
+
end
|
1061
|
+
end
|
1062
|
+
|
1063
|
+
end
|
data/lib/hashlab.rb
CHANGED
@@ -53,8 +53,8 @@ module HashLab
|
|
53
53
|
# :begin :radix26
|
54
54
|
def radix26(s)
|
55
55
|
x = 0
|
56
|
-
s.
|
57
|
-
x = x * 26 + b.
|
56
|
+
s.each_char do |b|
|
57
|
+
x = x * 26 + b.iord
|
58
58
|
end
|
59
59
|
return x
|
60
60
|
end
|
@@ -64,7 +64,7 @@ module HashLab
|
|
64
64
|
# string to hash, +n+ is the size of the hash table.
|
65
65
|
#
|
66
66
|
# Example:
|
67
|
-
# >> ?b.
|
67
|
+
# >> ?b.iord
|
68
68
|
# => 1
|
69
69
|
# >> h0("beer", 10)
|
70
70
|
# => 1
|
@@ -72,7 +72,7 @@ module HashLab
|
|
72
72
|
#--
|
73
73
|
# :begin :h0
|
74
74
|
def h0(s, n)
|
75
|
-
return s[0].
|
75
|
+
return s[0].iord % n
|
76
76
|
end
|
77
77
|
# :end :h0
|
78
78
|
|
@@ -81,7 +81,7 @@ module HashLab
|
|
81
81
|
# string to hash, +n+ is the size of the hash table.
|
82
82
|
#
|
83
83
|
# Example:
|
84
|
-
# >> ?b.
|
84
|
+
# >> ?b.iord * 26 + ?e.iord
|
85
85
|
# => 30
|
86
86
|
# >> h1("beer", 10)
|
87
87
|
# => 0
|
@@ -89,7 +89,7 @@ module HashLab
|
|
89
89
|
#--
|
90
90
|
# :begin :h1
|
91
91
|
def h1(s, n)
|
92
|
-
return ((s[0].
|
92
|
+
return ((s[0].iord * 26) + s[1].iord) % n
|
93
93
|
end
|
94
94
|
# :end :h1
|
95
95
|
|
@@ -114,7 +114,7 @@ module HashLab
|
|
114
114
|
# size +n+ (which has a default value of 10).
|
115
115
|
#
|
116
116
|
# Example:
|
117
|
-
# >> ?z.
|
117
|
+
# >> ?z.iord
|
118
118
|
# => 25
|
119
119
|
# >> h("zymurgy")
|
120
120
|
# => 5
|
@@ -124,7 +124,7 @@ module HashLab
|
|
124
124
|
#--
|
125
125
|
# :begin :h
|
126
126
|
def h(s, n = 10)
|
127
|
-
return s[0].
|
127
|
+
return s[0].iord % n
|
128
128
|
end
|
129
129
|
# :end :h
|
130
130
|
|
data/lib/randomlab.rb
CHANGED
@@ -298,46 +298,34 @@ and its suit (:spades, :hearts, etc).
|
|
298
298
|
|
299
299
|
@@outputform = :utf8
|
300
300
|
|
301
|
-
# Make a string to display a Card objects on the terminal.
|
302
|
-
|
303
|
-
# shows a spade, heart, diamond, or club is inserted into the string, otherwise a letter is
|
304
|
-
# used.
|
305
|
-
|
301
|
+
# Make a string to display a Card objects on the terminal.
|
302
|
+
|
306
303
|
def inspect
|
307
304
|
s = ""
|
308
305
|
s << case @rank
|
309
|
-
when :ace
|
310
|
-
when :king
|
311
|
-
when :queen
|
312
|
-
when :jack
|
313
|
-
when :ten
|
314
|
-
when :nine
|
315
|
-
when :eight
|
316
|
-
when :seven
|
317
|
-
when :six
|
318
|
-
when :five
|
319
|
-
when :four
|
320
|
-
when :three
|
321
|
-
when :two
|
306
|
+
when :ace then "A"
|
307
|
+
when :king then "K"
|
308
|
+
when :queen then "Q"
|
309
|
+
when :jack then "J"
|
310
|
+
when :ten then "10"
|
311
|
+
when :nine then "9"
|
312
|
+
when :eight then "8"
|
313
|
+
when :seven then "7"
|
314
|
+
when :six then "6"
|
315
|
+
when :five then "5"
|
316
|
+
when :four then "4"
|
317
|
+
when :three then "3"
|
318
|
+
when :two then "2"
|
322
319
|
end
|
323
|
-
if
|
324
|
-
if @@outputform == :utf8
|
325
|
-
s << case @suit
|
326
|
-
when :spades : "\xe2\x99\xa0"
|
327
|
-
when :hearts : "\xe2\x99\xa5"
|
328
|
-
when :clubs : "\xe2\x99\xa3"
|
329
|
-
when :diamonds : "\xe2\x99\xa6"
|
330
|
-
end
|
331
|
-
else
|
332
|
-
s << "!irb" + @suit.to_s.chop
|
333
|
-
end
|
334
|
-
else
|
320
|
+
if @@outputform == :utf8
|
335
321
|
s << case @suit
|
336
|
-
when :spades
|
337
|
-
when :hearts
|
338
|
-
when :clubs
|
339
|
-
when :diamonds
|
322
|
+
when :spades then "\xe2\x99\xa0"
|
323
|
+
when :hearts then "\xe2\x99\xa5"
|
324
|
+
when :clubs then "\xe2\x99\xa3"
|
325
|
+
when :diamonds then "\xe2\x99\xa6"
|
340
326
|
end
|
327
|
+
else
|
328
|
+
s << "!irb" + @suit.to_s.chop
|
341
329
|
end
|
342
330
|
return s
|
343
331
|
end
|
data/lib/rubylabs.rb
CHANGED
@@ -277,6 +277,13 @@ call <tt>a.random(:success)</tt> to get a value that is in the array +a+, or cal
|
|
277
277
|
|
278
278
|
end
|
279
279
|
|
280
|
+
def sort
|
281
|
+
tmp = self.clone
|
282
|
+
tmp.sort!
|
283
|
+
return tmp
|
284
|
+
end
|
285
|
+
|
286
|
+
|
280
287
|
# Return a value that is guaranteed to be in the array or not in the array,
|
281
288
|
# depending on the value of +outcome+. Pass <tt>:success</tt> to get a random
|
282
289
|
# value in the array, or pass <tt>:fail</tt> to get an item of the same type as the
|
@@ -1510,47 +1517,31 @@ they do for Array objects:
|
|
1510
1517
|
|
1511
1518
|
end # RubyLabs
|
1512
1519
|
|
1513
|
-
|
1514
|
-
|
1515
|
-
== Fixnum
|
1516
|
-
|
1517
|
-
When the RubyLabs module is loaded it defines a new method named +ord+ to the
|
1518
|
-
Fixnum class. In Ruby 1.8, using the <tt>[]</tt> operator to access items in a String object
|
1519
|
-
returns the ASCII value of a character. The +ord+ method defined here (and used by hash functions defined in hashlab.rb)
|
1520
|
-
maps the ASCII value of a letter to a number between 0 and 25.
|
1520
|
+
class String
|
1521
1521
|
|
1522
|
-
|
1523
|
-
object containing the binary or hexadecimal representation of an integer.
|
1524
|
-
#--
|
1525
|
-
NOTE: +ord+ is built in to Ruby 1.9, so this method will have to be renamed or reimplemented
|
1526
|
-
when RubyLabs is ported to 1.9.
|
1527
|
-
=end
|
1528
|
-
|
1529
|
-
class Fixnum
|
1530
|
-
|
1531
|
-
# If a number is the ASCII code for a letter from the Roman alphabet (upper or lower case,
|
1522
|
+
# If a character is a letter from the Roman alphabet (upper or lower case,
|
1532
1523
|
# in the range 'A' to 'Z') map it to a number between 0 and 25, otherwise just return the
|
1533
|
-
#
|
1524
|
+
# ASCII code of the letter.
|
1534
1525
|
#
|
1535
1526
|
# Example:
|
1536
|
-
# >> "Ducks!".
|
1527
|
+
# >> "Ducks!".each_char { |x| puts x.iord }
|
1537
1528
|
# 3
|
1538
1529
|
# 20
|
1539
1530
|
# 2
|
1540
1531
|
# 10
|
1541
1532
|
# 18
|
1542
1533
|
# 33
|
1543
|
-
|
1544
|
-
def
|
1545
|
-
if self >= ?a && self <= ?z
|
1546
|
-
self - ?a
|
1547
|
-
elsif self >= ?A && self <= ?Z
|
1548
|
-
self - ?A
|
1534
|
+
|
1535
|
+
def iord
|
1536
|
+
if self[0] >= ?a && self[0] <= ?z
|
1537
|
+
self[0].ord - ?a.ord
|
1538
|
+
elsif self[0] >= ?A && self[0] <= ?Z
|
1539
|
+
self[0].ord - ?A.ord
|
1549
1540
|
else
|
1550
|
-
self
|
1541
|
+
self[0].ord
|
1551
1542
|
end
|
1552
1543
|
end
|
1553
1544
|
|
1554
|
-
end #
|
1545
|
+
end # String
|
1555
1546
|
|
1556
1547
|
include RubyLabs
|
data/lib/spherelab.rb
CHANGED
@@ -406,8 +406,8 @@ to move in as smooth a circle as possible about a central point.
|
|
406
406
|
|
407
407
|
def track(val)
|
408
408
|
case val
|
409
|
-
when :off
|
410
|
-
when :on
|
409
|
+
when :off then @tracking = :off
|
410
|
+
when :on then @tracking = :track
|
411
411
|
else
|
412
412
|
puts "tracking is either :on or :off"
|
413
413
|
return nil
|
data/lib/tsplab.rb
CHANGED
@@ -1313,3 +1313,15 @@ module Enumerable
|
|
1313
1313
|
|
1314
1314
|
end
|
1315
1315
|
|
1316
|
+
# Ruby 2.0: String doesn't mix in Enumerable...
|
1317
|
+
|
1318
|
+
class String
|
1319
|
+
def each_permutation
|
1320
|
+
if block_given?
|
1321
|
+
self.chars.each_permutation { |p| yield p.join('') }
|
1322
|
+
else
|
1323
|
+
return self.chars.each_permutation.map { |p| p.join('') }
|
1324
|
+
end
|
1325
|
+
end
|
1326
|
+
end
|
1327
|
+
|
data/test/hash_test.rb
CHANGED
data/test/rubylabs_test.rb
CHANGED
@@ -107,15 +107,15 @@ class RubyLabsTest < Test::Unit::TestCase
|
|
107
107
|
assert_raise(NoMethodError) { pq[0] = 100 }
|
108
108
|
end
|
109
109
|
|
110
|
-
# '
|
111
|
-
#
|
110
|
+
# 'iord' method -- maps upper and lower case letters to 0..25, others return
|
111
|
+
# ASCII code of char
|
112
112
|
|
113
113
|
def test_09_ord
|
114
|
-
assert_equal 0, ?A.
|
115
|
-
assert_equal 25, ?Z.
|
116
|
-
assert_equal 0, ?a.
|
117
|
-
assert_equal 25, ?z.
|
118
|
-
assert_equal ?:.
|
114
|
+
assert_equal 0, ?A.iord
|
115
|
+
assert_equal 25, ?Z.iord
|
116
|
+
assert_equal 0, ?a.iord
|
117
|
+
assert_equal 25, ?z.iord
|
118
|
+
assert_equal ?:.iord, ?:.ord
|
119
119
|
end
|
120
120
|
|
121
121
|
end
|
metadata
CHANGED
@@ -1,46 +1,38 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubylabs
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 9
|
9
|
-
- 8
|
10
|
-
version: 0.9.8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- conery
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2016-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: rubygems-test
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
32
20
|
type: :runtime
|
33
|
-
|
34
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A set of modules for interactive experiments in an introductory computer
|
28
|
+
science class.
|
35
29
|
email: conery@cs.uoregon.edu
|
36
30
|
executables: []
|
37
|
-
|
38
31
|
extensions: []
|
39
|
-
|
40
|
-
extra_rdoc_files:
|
32
|
+
extra_rdoc_files:
|
41
33
|
- LICENSE
|
42
34
|
- README.rdoc
|
43
|
-
files:
|
35
|
+
files:
|
44
36
|
- .gemtest
|
45
37
|
- LICENSE
|
46
38
|
- README.rdoc
|
@@ -86,7 +78,6 @@ files:
|
|
86
78
|
- lib/bitlab.rb
|
87
79
|
- lib/demos.rb
|
88
80
|
- lib/elizalab.rb
|
89
|
-
- lib/encryptionlab.rb
|
90
81
|
- lib/hashlab.rb
|
91
82
|
- lib/introlab.rb
|
92
83
|
- lib/iterationlab.rb
|
@@ -101,7 +92,6 @@ files:
|
|
101
92
|
- lib/tsplab.rb
|
102
93
|
- test/bit_test.rb
|
103
94
|
- test/eliza_test.rb
|
104
|
-
- test/encryption_test.rb
|
105
95
|
- test/hash_test.rb
|
106
96
|
- test/iteration_test.rb
|
107
97
|
- test/mars_test.rb
|
@@ -114,41 +104,30 @@ files:
|
|
114
104
|
- test/tsp_test.rb
|
115
105
|
homepage: http://github.com/conery/rubylabs
|
116
106
|
licenses: []
|
117
|
-
|
107
|
+
metadata: {}
|
118
108
|
post_install_message:
|
119
109
|
rdoc_options: []
|
120
|
-
|
121
|
-
require_paths:
|
110
|
+
require_paths:
|
122
111
|
- lib
|
123
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
none: false
|
134
|
-
requirements:
|
135
|
-
- - ">="
|
136
|
-
- !ruby/object:Gem::Version
|
137
|
-
hash: 3
|
138
|
-
segments:
|
139
|
-
- 0
|
140
|
-
version: "0"
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
141
122
|
requirements: []
|
142
|
-
|
143
123
|
rubyforge_project: rubylabs
|
144
|
-
rubygems_version:
|
124
|
+
rubygems_version: 2.4.6
|
145
125
|
signing_key:
|
146
|
-
specification_version:
|
126
|
+
specification_version: 4
|
147
127
|
summary: Software and data for lab projects for Explorations in Computing.
|
148
|
-
test_files:
|
128
|
+
test_files:
|
149
129
|
- test/bit_test.rb
|
150
130
|
- test/eliza_test.rb
|
151
|
-
- test/encryption_test.rb
|
152
131
|
- test/hash_test.rb
|
153
132
|
- test/iteration_test.rb
|
154
133
|
- test/mars_test.rb
|
data/lib/encryptionlab.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
|
2
|
-
=begin rdoc
|
3
|
-
|
4
|
-
== Encryption Lab
|
5
|
-
|
6
|
-
Ruby implementation of Caesar cypher and RSA encryption.
|
7
|
-
|
8
|
-
=end
|
9
|
-
|
10
|
-
module RubyLabs
|
11
|
-
|
12
|
-
module EncryptionLab
|
13
|
-
|
14
|
-
=begin rdoc
|
15
|
-
Caesar cipher -- single letter substitution based on "rotating" each letter +n+ places.
|
16
|
-
=end
|
17
|
-
|
18
|
-
# :begin :caesar
|
19
|
-
def caesar(s, n = 3)
|
20
|
-
res = String.new
|
21
|
-
s.each_byte do |byte|
|
22
|
-
if byte >= ?a && byte <= ?z
|
23
|
-
res << rot(byte, ?a, n)
|
24
|
-
elsif byte >= ?A && byte <= ?Z
|
25
|
-
res << rot(byte, ?A, n)
|
26
|
-
else
|
27
|
-
res << byte
|
28
|
-
end
|
29
|
-
end
|
30
|
-
return res
|
31
|
-
end
|
32
|
-
# :end :caesar
|
33
|
-
|
34
|
-
def rot(char, base, n)
|
35
|
-
return ((char - base) + n) % 26 + base
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end # EncryptionLab
|
41
|
-
|
42
|
-
end # RubyLabs
|
data/test/encryption_test.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
require 'test_helper'
|
3
|
-
|
4
|
-
include EncryptionLab
|
5
|
-
|
6
|
-
class TestEncryption < Test::Unit::TestCase
|
7
|
-
|
8
|
-
def test_00_banner
|
9
|
-
print "\nEncryptionLab"
|
10
|
-
end
|
11
|
-
|
12
|
-
# Caesar cypher
|
13
|
-
|
14
|
-
def test_01_caesar
|
15
|
-
assert_equal caesar("abcdefghijklmnopqrstuvwxyz"), "defghijklmnopqrstuvwxyzabc"
|
16
|
-
assert_equal caesar("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), "DEFGHIJKLMNOPQRSTUVWXYZABC"
|
17
|
-
assert_equal caesar("abcdefghijklmnopqrstuvwxyz", 13), "nopqrstuvwxyzabcdefghijklm"
|
18
|
-
assert_equal caesar("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 13), "NOPQRSTUVWXYZABCDEFGHIJKLM"
|
19
|
-
assert_equal caesar("Et tu, Brute?"), "Hw wx, Euxwh?"
|
20
|
-
assert_equal caesar("Et tu, Brute?", 13), "Rg gh, Oehgr?"
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|