From: Wayne Blair Date: 2001-05-22T16:41:26+09:00 Subject: [ruby-talk:15535] Re: new xmp - please test Sorry for that brutal first post. In any case, I modified xmp.rb to fix the bug declared in its documentation, in which the report followed a statement's output. Please check this bug fix see if it should make its way into the repository. (Please also let me know a better procedure for posting this kind of thing.) Wayne Here is source to test, sans documentation. module Kernel XMP_VERSION = "2001-05-22" XMP_DEFAULT_OPTION = ["%l\n #%r\n", "\n # "] ###### Start code added by Wayne ###### def initBuffers @buffers = Array.new def @buffers.reset clear @currentIndex = -1 end def @buffers.incrementIndex @currentIndex += 1 push "" end def @buffers.write(str) if @currentIndex < 0 STDOUT << str else at(@currentIndex) << str end end end def setStdOut $defout = STDOUT end private :setStdOut def setBufferOut $defout = @buffers end private :setBufferOut ###### End code added by Wayne ###### def xmpsec(*a) puts *a; end def xmp(arg, fmt = XMP_DEFAULT_OPTION[0], sep=XMP_DEFAULT_OPTION[1]) if fmt print fmt ###### Begin code added by Wayne ###### initBuffers if @buffers==nil @buffers.reset setBufferOut ###### End code added by Wayne ###### fmt = fmt.to_s eval("___sep___ = #{sep.inspect}; ___res___ = []", TOPLEVEL_BINDING) eval(arg.gsub(/^(.*)\n?/){ @buffers.incrementIndex ### Added by Wayne %Q| begin ___res___ << "=> " + (#{$1}).inspect.gsub(/\n^/, ___sep___); rescue Exception ___res___ << "!! " + $!; end|}, TOPLEVEL_BINDING) setStdOut ### Added by Wayne arg.split(/\n/).each_with_index{|l,i| (puts "\n" ; next) if l =~ /^$/ res = fmt.gsub(/%l/, l) res = res.gsub(/%r/, eval("___res___", TOPLEVEL_BINDING)[i]) print res ### Added by Wayne - adds extra newline to any buffer that is printed print(@buffers[i] + "\n") if @buffers[i] && @buffers[i].length>0 } else print arg; # eval(arg, TOPLEVEL_BINDING) end return nil end alias __xmp__ xmp end if __FILE__ == $0 puts "## EXAMPLE 1" xmp "nil.to_i" puts "" puts "## EXAMPLE 2" xmp <<-EOS a = "The moon Fly" b = [a.split, "TO", "Me"] b = b.flatten b.sort! b.join(" ").capitalize EOS puts "" puts "## EXAMPLE 3" xmp <<-EOS, false class Foo attr :a, true end EOS xmp <<-EOS foo = Foo.new foo.a foo.a = 10 foo.b foo.a EOS end