[Milo Thurston <nospam / linacreschoolofdefence.org>, 2004-06-03 12.10 CEST]
[...]
> This runs but does not (of course) display the "please wait" part.
> Are there any sites similar to the above giving Ruby examples?
> Thanks.

I don't know about any sites. Here is an approach to display the "please
wait" message.

The problem is that cgi.<methods> create strings. When you do

cgi.out { cgi.html { cgi.body { cgi.h1 { ... } } } }

all these methods create strings and only when you have a string with the
whole homepage, it is sent to the client. So, if you want to flush the
output in the middle, you can't use these methods, but have to write the
tags manually.

#!/usr/local/bin/ruby
require "cgi"
require "thread"

cgi = CGI.new "html4"

header = "<h1>HEADER</h1>"
submit_buttons = "<p><i>here the submit buttons</i></p>"

# flush output after each print or puts:
$stdout.sync=true

# manually generate the page until the point where we want to display the
# "please wait" message:
puts cgi.header
puts
puts cgi.doctype
puts "<html><head><title>Your results</title></head><body>"
puts header
puts submit_buttons

# we will put the results of our long calculation in this variable:
results = nil

# this thread will do the calculation, and in the end, will put the results
# in the variable results:
Thread.new do
	# our long calculation is only sleep :)
	begin
		sleep 5
		results = "<table border=1><tr><td>result 1</td>" +
			"<td>result 2</td></tr></table>"
	rescue
		results = "<b>BIG ERROR</b>"
	end
end

# show the message...
print '<b id="PLEASEWAIT">Please wait ...'

# and wait until there is any result. Meanwhile we show nice progression
# dots...
while !results
	print " ."
	sleep 0.5
end
# now we have the results
puts "</b>"

# this javascript is to remove the "please wait" message.
# works in mozilla :)
puts '<script>n=document.getElementById("PLEASEWAIT");' +
	'n.parentNode.removeChild(n)</script>'

puts results

puts "</body></html>"