以下は努力の記録

 #!/bin/sh
 exec 2>&1 ruby -Ke -S -x $0 "$@" | ruby -e &#39;require "cgi";((a=$stdin.read) !~ /^content/i)?(puts "content-type: text/html\n\n<h1>ERROR</h1><pre>#{CGI::escapeHTML("#{a}")}</pre>"):(print a)&#39;
 #! ruby
 #!/bin/sh
 exec 2>&1 ruby -Ke -S -x $0 "$@" | ruby -e &#39;require "cgi";(gets !~ /^content/i)?(puts "content-type: text/html\n\n<h1>ERROR</h1><pre>>#{CGI::escapeHTML("#{$_}#{$stdin.read}")}</pre>"):(print $_,$stdin.read)&#39;
 #! ruby
 #!/bin/sh
 exec 2>&1 ruby -Ke -S -x $0 "$@" | ./error.rb
 #! ruby
 puts &#39;

パスの通った所に以下を error.rbとして保存する

 #!/usr/bin/ruby
 require &#39;cgi&#39;
 if gets !~ /content-type/i
   print <<EOS
 content-type: text/html
 
 <h1>ERROR</h1>
 <pre>#{CGI::escapeHTML($_)}#{CGI::escapeHTML($stdin.read)}</pre>
 EOS
 else
   print $_,$stdin.read
 end

 -Sオプションを使って まずhtmlヘッダを出力し、標準エラーを標準入力へリダイレクトした後スクリプトを実行する

 #!/bin/sh
 echo "content-type: text/html"
 echo
 exec 2>&1 ruby -S -x $0 "$@"
 #! ruby
 begin
   puts <<-EOS
   aaaaa
   EOS
 end

こんなの考えてみたけどヘッダがこんなに長いのは気持ち悪くなってきたので却下。

 !/bin/sh
 echo "content-type: text/html"
 echo
 export a=`uuidgen`
 command  ruby -S -x $0 "$@"  >& /tmp/$a
 echo $a
 if [ -e /tmp/$a ]; then
   cat /tmp/$a| sed &#39;s/$/<br>/g&#39;
   rm /tmp/$a
 fi
 exit
 end
 #! ruby
 begin
   ret=0
   10.times{|i| ret+=i}
   require &#39;kakasi&#39;
   puts <<-EOS
   aaaaa
   EOS
 end