Module: RubyLabs
- Defined in:
- lib/demos.rb,
lib/bitlab.rb,
lib/tsplab.rb,
lib/hashlab.rb,
lib/lifelab.rb,
lib/marslab.rb,
lib/elizalab.rb,
lib/introlab.rb,
lib/rubylabs.rb,
lib/sievelab.rb,
lib/iterationlab.rb,
lib/recursionlab.rb,
lib/encryptionlab.rb
Overview
Encryption Lab
Ruby implementation of Caesar cypher and RSA encryption.
Defined Under Namespace
Modules: BitLab, Canvas, Demos, ElizaLab, EncryptionLab, HashLab, IntroLab, IterationLab, LifeLab, MARSLab, RecursionLab, SieveLab, Source, TSPLab Classes: PriorityQueue, TestArray
Instance Method Summary collapse
-
#count(&f) ⇒ Object
Monitor the execution of a block of code, which can be any Ruby expression enclosed in braces.
-
#hello ⇒ Object
A simple ‘hello world’ method to test the installation.
-
#log2(x) ⇒ Object
Compute the logarithm (base 2) of a number
x
. -
#max(a, b) ⇒ Object
Return the larger of
a
andb
(determined by callinga < b
). -
#min(a, b) ⇒ Object
Return the smaller of
a
andb
(determined by callinga < b
). -
#TestArray(n, type = nil) ⇒ Object
Make a new TestArray object (equivalent to calling
TestArray.new
). -
#time(&f) ⇒ Object
Measure the execution time of a block of code, which can be any Ruby expression enclosed in braces after the method name.
-
#trace(&f) ⇒ Object
Monitor the execution of a block of code, which can be any Ruby expression enclosed in braces.
Instance Method Details
#count(&f) ⇒ Object
Monitor the execution of a block of code, which can be any Ruby expression enclosed in braces. If a counting probe (see Source#probe) is attached to any lines of code evaluated by the block this method will return the total number of times those lines are executed.
See also RubyLabs#trace.
Example – count the number of times line 2 in the method named less
is executed during a call to isort
:
>> Source.probe("less", 2, :count)
=> true
>> count { isort(cars) }
=> 8
:call-seq:
count { f } => Fixnum
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/rubylabs.rb', line 156 def count(&f) counter = 0 set_trace_func proc { |event, file, line, id, binding, classname| if expr = Source.probing(file, id, line) counter += 1 if expr == :count end } f.call set_trace_func nil return counter end |
#hello ⇒ Object
A simple ‘hello world’ method to test the installation.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rubylabs.rb', line 40 def hello puts "Hello, you have successfully installed RubyLabs!" begin Canvas.init(200, 200, "RubyLabs::Hello") Canvas::Text.new("Hello from RubyLabs!",40,90) rescue Exception => e puts e puts "Did you install Tk, the software module used to display graphics? See your Lab Manual for installtion instructions..." end return true end |
#log2(x) ⇒ Object
Compute the logarithm (base 2) of a number x
.
:call-seq:
log2(x) => Float
78 79 80 |
# File 'lib/rubylabs.rb', line 78 def log2(x) log(x) / log(2.0) end |
#max(a, b) ⇒ Object
Return the larger of a
and b
(determined by calling a < b
).
:call-seq:
max(a,b) => Object
58 59 60 |
# File 'lib/rubylabs.rb', line 58 def max(a,b) a < b ? b : a end |
#min(a, b) ⇒ Object
Return the smaller of a
and b
(determined by calling a < b
).
:call-seq:
min(a,b) => Object
68 69 70 |
# File 'lib/rubylabs.rb', line 68 def min(a,b) a < b ? a : b end |
#TestArray(n, type = nil) ⇒ Object
Make a new TestArray object (equivalent to calling TestArray.new
).
Examples:
>> TestArray(5)
=> [65, 79, 60, 88, 30]
>> TestArray(5, :cars)
=> ["mini", "opel", "chevrolet", "isuzu", "cadillac"]
:call-seq:
TestArray(n, type) => Array
409 410 411 |
# File 'lib/rubylabs.rb', line 409 def TestArray(n, type = nil) TestArray.new(n, type) end |
#time(&f) ⇒ Object
Measure the execution time of a block of code, which can be any Ruby expression enclosed in braces after the method name. The return value is the number of seconds required to evaluate the block. Any output produced by the block is discarded.
Example:
>> time { sieve(1000) }
=> 0.014921
:call-seq:
time { f } => Float
95 96 97 98 99 |
# File 'lib/rubylabs.rb', line 95 def time(&f) tstart = Time.now f.call return Time.now - tstart end |
#trace(&f) ⇒ Object
Monitor the execution of a block of code, which can be any Ruby expression enclosed in braces. Sets up a callback method which checks to see if a probe has been attached to a line in a method called by the block. See Source#probe for information about attaching probes. If a probe is attached, the expression associated with the probe is evaluated. The value returned from trace is the value returned by the block.
See also RubyLabs#count.
Example – call a method named brackets
to print brackets around array elements as the arry is being sorted by a call to isort
:
>> Source.probe( "isort", 5, "puts brackets(a,i)" )
=> true
>> trace { isort(cars) }
mazda [ford bmw saab chrysler]
ford mazda [bmw saab chrysler]
bmw ford mazda [saab chrysler]
...
=> ["bmw", "chrysler", "ford", "mazda", "saab"]
:call-seq:
trace { f } => Object
– Debugging aid: put this line at the front of the trace_func:
p [event, file, line, id, binding, classname]
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rubylabs.rb', line 128 def trace(&f) set_trace_func proc { |event, file, line, id, binding, classname| if expr = Source.probing(file, id, line) eval(expr, binding) if expr != :count end } res = f.call set_trace_func nil res end |