Permalink
Browse files

Updated to work with late model Ruby, a few changes to gems

  • Loading branch information...
Russ Olsen
Russ Olsen committed Jun 21, 2013
1 parent a36a23f commit 705db75509c0a046c122ee5231813eea7b2182e8
Showing 489 changed files with 10,881 additions and 31 deletions.
View
@@ -0,0 +1,27 @@
+
+Targets = %w{ messy_doc.rb spec out.odt}
+
+require '../utils/tasks.rb'
+
+CLEAN.include('messy_doc.rb')
+
+
+file 'messy_doc.rb' do |f|
+ code = File.read( '../code/doc3.rb' )
+ code = code.gsub( / /, "\t" )
+ new_code = ''
+ count = 1
+ code.each_char do |ch|
+ unless ch == "\t"
+ new_code << ch
+ else
+ if (count % 2) == 0
+ new_code << ' '
+ else
+ new_code << ' '
+ end
+ end
+ count += 1
+ end
+ File.open( 'messy_doc.rb', 'w') {|f| f.print(new_code)}
+end
View
@@ -0,0 +1,17 @@
+require '../utils/rspec_utils'
+require 'ex_case'
+
+describe "The example case statements" do
+ it "should produce the right output with semicolons" do
+ (output_of { case_semi( 'War And Peace' ) }).should == "Tolstoy\n"
+ (output_of { case_semi( 'Romeo And Juliet' ) }).should == "Shakespeare\n"
+ (output_of { case_semi( 'Whatever' ) }).should == "Don't know\n"
+ end
+
+ it "should produce the right output with semicolons" do
+ (output_of { case_no_semi( 'War And Peace' ) }).should == "Tolstoy\n"
+ (output_of { case_no_semi( 'Romeo And Juliet' ) }).should == "Shakespeare\n"
+ (output_of { case_no_semi( 'Whatever' ) }).should == "Don't know\n"
+ end
+
+end
View
@@ -0,0 +1,16 @@
+# Class that models a plain text document, complete with title
+# and author:
+#
+# doc = Document.new( 'Hamlet', 'Shakespeare', 'To be or...' )
+# puts doc.title
+# puts doc.author
+# puts doc.content
+#
+# Document instances know how to parse their content into words:
+#
+# puts doc.words
+# puts doc.word_count
+#
+class Document
+ # class omitted...
+end
View
@@ -0,0 +1,25 @@
+# Class which represents a document
+class Document
+
+ attr_accessor :title, :author, :content
+
+ # Initialize the document, passing in the document tile,
+ # the document author, and the text of the document
+ def initialize(title, author, content)
+ @title = title
+ @author = author
+ @content = content
+ end
+
+ # Return the words in the document
+
+ def words
+ @content.split
+ end
+
+ # Return the number of words in the document
+
+ def word_count
+ words.size
+ end
+end
View
@@ -0,0 +1,6 @@
+case Document
+ def count_words_in( the_string ) ##(main
+ the_words = the_string.split
+ the_words.size
+ end ##main)
+end
View
@@ -0,0 +1,23 @@
+require '../utils/rspec_utils'
+
+describe 'folding lines' do
+
+ it 'should demo a one liner with braces' do
+ out = output_of {
+ 10.times { |n| puts "The number is #{n}" } ##+brace
+ }
+ out.should match( /The number.*0.*1.*2.*9$/m )
+ end
+
+ it 'should demo the do end version' do
+ out = output_of {
+ 10.times do |n| ##(do
+ puts "The number is #{n}"
+ puts "Twice the number is #{n*2}"
+ end ##do)
+ }
+ out.should match( /The number.*0.*1.*2.*9$/m )
+ end
+end
+
+
View
@@ -0,0 +1,24 @@
+##
+
+def case_no_semi(title)
+##start no_semi
+ case title
+ when 'War And Peace'
+ puts 'Tolstoy'
+ when 'Romeo And Juliet'
+ puts 'Shakespeare'
+ else
+ puts "Don't know"
+ end
+##end no_semi
+end
+
+def case_semi(title)
+##start semi
+ case title;
+ when 'War And Peace'; puts 'Tolstoy'
+ when 'Romeo And Juliet'; puts 'Shakespeare'
+ else; puts "Don't know"
+ end
+##end semi
+end
View
@@ -0,0 +1,56 @@
+# Author: Russ Olsen ##(background
+# Copyright 2010 Russ Olsen
+#
+# Document: An example Ruby class ##background)
+
+
+# Class that models a plain text document, complete with title ##(with_example
+# and author:
+#
+# doc = Document.new( 'Hamlet', 'Shakespeare', 'To be or...' )
+# puts doc.title
+# puts doc.author
+# puts doc.content
+#
+# Document instances know how to parse their content into words:
+#
+# puts doc.words
+# puts doc.word_count
+#
+class Document
+ # class omitted...
+end ##with_example)
+
+
+
+ # Using ngram analysis, compute the probability ##(explain
+ # that this document and the one passed in were
+ # written by the same person. This algorithm is
+ # known to be valid for American English and will
+ # probably work for British and Canadian English.
+ #
+ def same_author_probability( other_document )
+ # Implementation left as an exercise for the reader...
+ end ##explain)
+
+describe 'comments examples' do
+ it 'how to example should just work' do
+ same_author_probability( 9999 )
+ end
+
+ it 'should also explain good one liners' do
+ divisor = 0
+
+ block = lambda {
+ return 0 if divisor == 0 # Avoid division by zero ##+good_one
+ }
+
+ block.call.should == 0
+ end
+
+ it 'should explain bad one liners' do
+ count = 88
+ count += 1 # Add one to count ##+bad_one
+ count.should == 89
+ end
+end
View
@@ -0,0 +1,10 @@
+
+describe 'constant examples' do
+ it 'should work' do
+ FurlongsPerFortnight = 0.0001663 ##+camel
+ ANTLERS_PER_MALE_MOOSE = 2 ##+worm
+
+ defined?(FurlongsPerFortnight).should == 'constant'
+ defined?(ANTLERS_PER_MALE_MOOSE).should == 'constant'
+ end
+end
View
@@ -0,0 +1,26 @@
+require '../utils/rspec_utils'
+require '../code/doc3'
+
+
+describe 'using semicolons' do
+
+ it 'should let you stack statements' do
+ doc = Document.new( 'title', 'russ', '')
+
+ out = output_of {
+ puts doc.title; puts doc.author ##+two_puts
+ }
+ out.should match( /title.*russ/m )
+ end
+
+ it 'should let you define a one line class' do
+ class DocumentException < Exception; end ##+doc_exception
+ DocumentException.instance_of?(Class).should == true
+ end
+
+ it 'should let you define a method in one line' do
+ def method_to_be_overriden; end ##+one_line_method
+ method_to_be_overriden
+ end
+
+end
View
@@ -0,0 +1,51 @@
+require '../utils/rspec_utils'
+require '../code/doc3'
+
+describe 'staying out of trouble examples' do
+
+ it 'should have a good do end loop' do
+ doc = Document.new( '', '', 'hello world' )
+
+ out = output_of {
+ doc.words.each do |word| ##(fold_do
+ puts word
+ end ##fold_do)
+ }
+
+ out.should match( /hello\nworld\n/m )
+ end
+
+ it 'should have a good {} loop' do
+ doc = Document.new( '', '', 'hello world' )
+
+ out = output_of {
+ doc.words.each { |word| puts word } ##+braces
+ }
+
+ out.should match( /hello\nworld\n/m )
+ end
+
+ it 'should have a good no paren example' do
+ doc = Document.new( 'title', 'russ', 'hello world' )
+
+ out = output_of {
+ puts doc.author ##+puts_no_paren
+ }
+
+ out.should == "russ\n"
+ end
+
+ it 'should have a good no paren defined?' do
+ doc = Document.new( 'title', 'russ', 'hello world' )
+ (
+ doc.instance_of? Document ##+instance_of
+ ).should == true
+ end
+
+ it 'should have a good example of a complicated expression' do
+ doc = Document.new( 'title', 'russ', 'hello world' )
+ puts doc.instance_of? self.class.superclass.class ##+complex_1
+ puts doc.instance_of?( self.class.superclass.class ) ##+complex_2
+ end
+
+end
View
@@ -0,0 +1,17 @@
+class Document
+ attr_accessor :title, :author, :content
+
+ def initialize(title, author, content)
+ @title = title
+ @author = author
+ @content = content
+ end
+
+ def words
+ @content.split
+ end
+
+ def word_count
+ words.size
+ end
+end
View
13 01/md
@@ -0,0 +1,13 @@
+class Document
+ attr_accessor :title, :author, :content
+
+ def initialize(title, author, content)
+ @title = title
+ @author = author
+ @content = content
+ end
+
+ def words
+ @content.split
+ end
+end
View
@@ -0,0 +1,42 @@
+# Some supporting stuff
+
+@content = 'It was a dark and stormy night'
+
+
+ def find_document( title, author ) ##(find_parens
+ # Body omitted...
+ end
+
+ # ...
+
+ find_document( 'Frankenstein', 'Shelley' ) ##find_parens)
+
+
+
+ def find_document title, author ##(find_no_parens
+ # Body omitted...
+ end
+
+ # ...
+
+ find_document 'Frankenstein', 'Shelley' ##find_no_parens)
+
+
+ puts 'Look Ma, no parentheses!' ##+puts_no_parens
+
+ def words ##(words_no_parens
+ @content.split
+ end ##words_no_parens)
+
+ def words() ##(words_parens
+ @content.split()
+ end ##words_parens)
+
+
+ if ( words.size < 100 ) ##(if_parens
+ puts 'The document is not very long.'
+ end ##if_parens)
+
+ if words.size < 100 ##(if_no_parens
+ puts 'The document is not very long.'
+ end ##if_no_parens)
View
@@ -0,0 +1 @@
+require '../utils/tasks.rb'
Oops, something went wrong.

0 comments on commit 705db75

Please sign in to comment.