 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Hiding Setup and Cleanup in a Block Method | ||
|---|---|---|
| Code | Expected | Actual | 
| def between_setup_and_cleanup
  setup()
  begin
   yield
  finally
    cleanup()
  end
end
def write_html(out, doctype=nil)
  doctype ||= %{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
           "http://www.w3.org/TR/html4/loose.dtd">}
  out.puts doctype
  out.puts '<html>'
  begin
    yield out
  ensure
    out.puts '</html>'
  end
end
write_html($stdout) do |out| 
  out.puts '<h1>Sorry, the Web is closed.</h1>'
end | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <h1>Sorry, the Web is closed.</h1> </html> | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
           "http://www.w3.org/TR/html4/loose.dtd">
<html>
<h1>Sorry, the Web is closed.</h1>
</html> | 
| open('output.txt', 'w') do |out| 
  out.puts 'Sorry, the filesystem is also closed.'
end
Content-Type: text/html
Content-Length: 137
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<HTML><H1>Sorry, the Web is closed.</H1></HTML>
require 'rubygems'
require 'builder'
xml = Builder::XmlMarkup.new.message({'type' => 'apology'}) do |b|
  b.content('Sorry, Web Services are closed.')
end
puts xml | <message type="apology"> <content>Sorry, Web Services are closed.</content> </message> | Error! (Exception?) Here's stdout:
SyntaxError: compile error
(irb):26: parse error, unexpected ':', expecting $
Content-Type: text/html
             ^
	from (irb):26
SyntaxError: compile error
(irb):27: parse error, unexpected ':', expecting $
Content-Length: 137
               ^
	from (irb):27
SyntaxError: compile error
(irb):28: parse error, unexpected '<'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 ^
	from (irb):28
 |