Permalink
Browse files

Add code from book

  • Loading branch information...
1 parent 762ce24 commit 3059c0a69c690b0283efe6d65c3669fba2f3bae8 @l4rk l4rk committed Jun 23, 2009
View
Binary file not shown.
View
@@ -0,0 +1,24 @@
+
+
+public class Empty
+{
+
+ public boolean isEmpty(String s)
+ {
+ return s.length() == 0;
+ }
+
+ public void doit(String s)
+ {
+ System.out.println( "For string [" + s + "] empty = " + isEmpty(s) );
+ }
+
+ public static void main(String[] args)
+ {
+ Empty empty = new Empty();
+
+ empty.doit("foo");
+ empty.doit("bar");
+ empty.doit("");
+ }
+}
View
@@ -0,0 +1 @@
+require 'ex7_empty'
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+
+require '../example'
+
+example %q{
+
+require 'webrick'
+
+class HelloServer < WEBrick::GenericServer
+ def run(socket)
+ socket.print('Hello TCP/IP world')
+ end
+end
+
+
+
+
+
+s = HelloServer.new(:Port => 2000)
+trap("INT"){ s.shutdown}
+s.start
+
+}
View
@@ -0,0 +1,20 @@
+
+class Report
+ def initialize
+ @title = 'Monthly Report'
+ @text = ['Things are going', 'really, really well.']
+ end
+
+ def output_report
+ puts('<html>')
+ puts(' <head>')
+ puts(" <title>#{@title}</title>")
+ puts(' </head>')
+ puts(' <body>')
+ @text.each do |line|
+ puts(" <p>#{line}</p>" )
+ end
+ puts(' </body>')
+ puts('</html>')
+ end
+end
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+require '../example'
+
+
+require 'ex1_report'
+
+example %q{
+
+report = Report.new
+report.output_report
+
+}
@@ -0,0 +1,36 @@
+
+
+class Report
+ def initialize
+ @title = 'Monthly Report'
+ @text = ['Things are going', 'really, really well.']
+ end
+
+ def output_report(format)
+ if format == :plain
+ puts("*** #{@title} ***")
+ elsif format == :html
+ puts('<html>')
+ puts(' <head>')
+ puts(" <title>#{@title}</title>")
+ puts(' </head>')
+ puts(' <body>')
+ else
+ raise "Unknown format: #{format}"
+ end
+
+ @text.each do |line|
+ if format == :plain
+ puts(line)
+ else
+ puts(" <p>#{line}</p>" )
+ end
+ end
+
+ if format == :html
+ puts(' </body>')
+ puts('</html>')
+ end
+ end
+end
+
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+require 'test/unit'
+require 'pp'
+require 'stringio'
+
+require 'ex2_hacked_report'
+
+
+class HackedTest < Test::Unit::TestCase
+
+ def test_hacked_report
+ r = Report.new
+ output = capture_output { r.output_report(:plain) }
+puts output
+ assert /Things/ =~ output
+
+ output = capture_output { r.output_report(:html) }
+puts output
+ assert /<html>/ =~ output
+ assert /<title>Monthly/ =~ output
+ assert_raise(RuntimeError){ r.output_report(:pdf) }
+
+ end
+
+ def capture_output(&block)
+ output = StringIO.new
+ begin
+ $stdout = output
+ block.call
+ ensure
+ $stdout = STDOUT
+ end
+ output.string
+ end
+
+end
+
+
+
@@ -0,0 +1,101 @@
+
+class Report
+ def initialize
+ @title = 'Monthly Report'
+ @text = ['Things are going', 'really, really well.']
+ end
+
+ def output_report
+ output_start
+ output_head
+ output_body_start
+ output_body
+ output_body_end
+ output_end
+ end
+
+ def output_body
+ @text.each do |line|
+ output_line(line)
+ end
+ end
+
+ def output_start
+ raise 'Called abstract method: output_start'
+ end
+
+ def output_head
+ raise 'Called abstract method: output_head'
+ end
+
+ def output_body_start
+ raise 'Called abstract method: output_body_start'
+ end
+
+ def output_line(line)
+ raise 'Called abstract method: output_line'
+ end
+
+ def output_body_end
+ raise 'Called abstract method: output_body_end'
+ end
+
+ def output_end
+ raise 'Called abstract method: output_end'
+ end
+end
+
+
+
+class HTMLReport < Report
+ def output_start
+ puts('<html>')
+ end
+
+ def output_head
+ puts(' <head>')
+ puts(" <title>#{@title}</title>")
+ puts(' </head>')
+ end
+
+ def output_body_start
+ puts('<body>')
+ end
+
+ def output_line(line)
+ puts(" <p>#{line}</p>")
+ end
+
+ def output_body_end
+ puts('</body>')
+ end
+
+ def output_end
+ puts('</html>')
+ end
+end
+
+class PlainTextReport < Report
+ def output_start
+ end
+
+ def output_head
+ puts("**** #{@title} ****")
+ puts
+ end
+
+ def output_body_start
+ end
+
+ def output_line(line)
+ puts line
+ end
+
+ def output_body_end
+ end
+
+ def output_end
+ end
+end
+
+
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+require '../example'
+require 'ex3_template_report'
+
+example %q{
+report = HTMLReport.new
+report.output_report
+
+report = PlainTextReport.new
+report.output_report
+
+}
@@ -0,0 +1,86 @@
+
+class Report
+ def initialize
+ @title = 'Monthly Report'
+ @text = ['Things are going', 'really, really well.']
+ end
+
+ def output_report
+ output_start
+ output_head
+ output_body_start
+ @text.each do |line|
+ output_line(line)
+ end
+ output_body_end
+ output_end
+ end
+
+ def output_start
+ end
+
+ def output_head
+ raise 'Called abstract method: output_head'
+ end
+
+ def output_body_start
+ end
+
+ def output_line(line)
+ raise 'Called abstract method: output_line'
+ end
+
+ def output_body_end
+ end
+
+ def output_end
+ end
+end
+
+
+
+class HTMLReport < Report
+ def output_start
+ puts('<html>')
+ end
+
+ def output_head
+ puts(' <head>')
+ puts(" <title>#{@title}</title>")
+ puts(' </head>')
+ end
+
+ def output_body_start
+ puts('<body>')
+ end
+
+ def output_line(line)
+ puts(" <p>#{line}</p>")
+ end
+
+ def output_body_end
+ puts('</body>')
+ end
+
+ def output_end
+ puts('</html>')
+ end
+end
+
+class PlainTextReport < Report
+ def output_start
+ end
+
+ def output_head
+ puts("**** #{@title} ****")
+ puts
+ end
+
+ def output_line(line)
+ puts(line)
+ end
+
+ def output_end
+ end
+end
+
Oops, something went wrong.

0 comments on commit 3059c0a

Please sign in to comment.