irb's xmp is an ``example printer''---that is, a pretty-printer that
shows the value of each expression as it is run (much like the script
we wrote to format the examples in this book). There is also another
stand-alone xmp in the archives.
xmp can be used as follows:
require "irb/xmp"
xmp <<END
artist = "Doc Severinsen"
artist
END
|
produces:
[pwd:/tc/work/ruby/ProgrammingRuby/latex]
artist = "Doc Severinsen"
==>"Doc Severinsen"
artist
==>"Doc Severinsen"
|
Or, it can be used as an object instance. Used in this fashion, the
object maintains context between invocations:
require "irb/xmp"
x = XMP.new
x.puts <<END
artist = "Louis Prima"
END
x.puts <<END
artist
END
|
produces:
[pwd:/tc/work/ruby/ProgrammingRuby/latex]
artist = "Louis Prima"
==>"Louis Prima"
artist
==>"Louis Prima"
|
You can explicitly provide a binding with either form; otherwise, xmp
uses the caller's environment.
xmp code_string, abinding
XMP.new(abinding)
|
Note that xmp does not work with multithreading.