Visible Workings > Ruby Code

Various Ruby Code
Brian Marick

       

A ruby tracing package

Ruby-trace is a package used by programmers, testers, server administrators and others to understand what happened during the execution of a Ruby program. It requires programmers to add tracing statements like the following to the code.

$trace.announce "Using alternate capability file '#{chosen}'."

Read more...

Dynamically scoped (fluid) variables

Class Fluid provides dynamically scoped ("fluid") variables modeled after those of Common Lisp. It also gives you a convenient way to reversibly change the values of globals.

Suppose that you want to change the value of $defout temporarily. Here's how:

require 'fluid'

Fluid.let(["$defout", File.open("logfile", "w"), :close]) {
  puts "This will go to 'logfile'."
}
puts "This will not."

Notice that the IO stream is closed after the original value is restored.

Fluid.let can also operate on its own kind of variables:

require 'fluid'

Fluid.let([:var1, 1]) {
  puts(Fluid.var1)  # prints 1
}
puts(Fluid.var1)    # error - Fluid.var1 does not exist

Fluid.var1 is much like a global, but whenever you see it in code, you know that code is intended to be wrapped inside a Fluid.let.

Read more...