diff --git a/spec_helper.rb b/spec_helper.rb index 2d5c93d..0f63c0c 100644 --- a/spec_helper.rb +++ b/spec_helper.rb @@ -2,4 +2,7 @@ config.expect_with :rspec do |c| c.syntax = [:should, :expect] end + config.mock_with :rspec do |mocks| + mocks.syntax = :should + end end \ No newline at end of file diff --git a/week4/class_materials/odd_number.rb b/week4/class_materials/odd_number.rb new file mode 100644 index 0000000..1c86876 --- /dev/null +++ b/week4/class_materials/odd_number.rb @@ -0,0 +1,18 @@ +class OddNumber + include Comparable + attr_accessor :value + + def initialize value + @value = value + end + + def succ + return OddNumber.new(@value + 1) if @value.even? + OddNumber.new(@value + 2) + end + + def <=> other + @value <=> other.value + end +end + diff --git a/week4/class_materials/resources.txt b/week4/class_materials/resources.txt new file mode 100644 index 0000000..725756d --- /dev/null +++ b/week4/class_materials/resources.txt @@ -0,0 +1,3 @@ +Blocks, Procs, Lambdas, and Closures: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ + + diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb new file mode 100644 index 0000000..67b7681 --- /dev/null +++ b/week4/class_materials/timer.rb @@ -0,0 +1,8 @@ +class Timer + def self.time_code(n=1) + start_time = Time.now + n.times{yield} + end_time = Time.now + (end_time - start_time) / n.to_f + end +end \ No newline at end of file diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb new file mode 100644 index 0000000..e7c83b7 --- /dev/null +++ b/week4/class_materials/timer_spec.rb @@ -0,0 +1,33 @@ +require_relative '../../spec_helper.rb' +require "#{File.dirname(__FILE__)}/timer" + +describe Timer do + + it "should report the time difference" do + Time.stub(:now).and_return(0,3) + time_difference = Timer.time_code do + end + time_difference.should be_within(0.1).of(3.0) + end + + it "should run our code" do + flag = false + Timer.time_code do + flag = true + end + flag.should be_true + end + + it "should run our code multiple times" do + counter = 0 + result = Timer.time_code(17) {counter += 1} + counter.should equal 17 + end + + it "should give the average time" do + Time.stub(:now).and_return(0,10) + result = Timer.time_code(10) { } + result.should be_within(0.1).of(1) + end + +end diff --git a/week4/class_materials/week4.key b/week4/class_materials/week4.key new file mode 100644 index 0000000..ac3687b Binary files /dev/null and b/week4/class_materials/week4.key differ diff --git a/week4/class_materials/week4.pdf b/week4/class_materials/week4.pdf new file mode 100644 index 0000000..30c75fa Binary files /dev/null and b/week4/class_materials/week4.pdf differ diff --git a/week4/exercises/monsters.rb b/week4/exercises/monsters.rb new file mode 100644 index 0000000..e86bbf6 --- /dev/null +++ b/week4/exercises/monsters.rb @@ -0,0 +1,53 @@ +$monsters = [] + +$monsters << { + :name => 'Zombie', + :nocturnal => false, + :dangers => ['bites', 'scratches'], + :vulnerabilities => ['fire', 'decapitation'], + :legs => 2 +} +$monsters << { + :name => 'Mummy', + :nocturnal => false, + :dangers => ['bites', 'scratches', 'curses'], + :vulnerabilities => ['fire', 'decapitation', 'cats'], + :legs => 2 +} +$monsters << { + :name => 'Vampire', + :nocturnal => true, + :dangers => ['bites', 'hypnosis'], + :vulnerabilities => ['wood', 'decapitation', 'crosses', 'holy_water', 'garlic', 'daylight'], + :legs => 2 +} +$monsters << { + :name => 'Werewolf', + :nocturnal => true, + :dangers => ['bites', 'scratches'], + :vulnerabilities => ['silver'], + :legs => 4 +} +$monsters << { + :name => 'Blob', + :nocturnal => false, + :dangers => ['suffocation'], + :vulnerabilities => ['CO2', 'ice', 'cold'], + :legs => 0 +} + +puts "How many monsters are nocturnal?" +puts $monsters.count{|m| m[:nocturnal]} + +puts "What are the names of the monsters that are nocturnal?" +puts $monsters.select{|m| m[:nocturnal]}.map{|m| m[:name]} + +puts "How many legs do all our monsters have?" +puts $monsters.map{|m| m[:legs]}.inject(:+) + +puts "What are the 2 most common dangers and vulnerabilities of our monsters?" +puts "2 most common dangers:" +puts $monsters.map{|m| m[:dangers]}.flatten.inject(Hash.new(0)){|h,d| h[d] += 1; h}.sort_by{|k,v| v}[-2..-1].map{|a| a[0]} +puts "2 most common vulnerabilities:" +puts $monsters.map{|m| m[:vulnerabilities]}.flatten.inject(Hash.new(0)){|h,v| h[v] += 1; h}.sort_by{|k,v| v}[-2..-1].map{|a| a[0]} + diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt new file mode 100644 index 0000000..ffaf215 --- /dev/null +++ b/week4/homework/questions.txt @@ -0,0 +1,13 @@ +Please Read: +Chapter 10 Basic Input and Output +The Rake Gem: http://rake.rubyforge.org/ + +1. How does Ruby read files? + +2. How would you output "Hello World!" to a file called my_output.txt? + +3. What is the Directory class and what is it used for? + +4. What is an IO object? + +5. What is rake and what is it used for? What is a rake task? diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb new file mode 100644 index 0000000..993cd49 --- /dev/null +++ b/week4/homework/worker_spec.rb @@ -0,0 +1,37 @@ +require_relative '../../spec_helper.rb' +require "#{File.dirname(__FILE__)}/worker" + +describe Worker do + + it "executes a block and returns a string" do + result = Worker.work do + "hello" + end + result.should == "hello" + end + + it "executes a block and returns a number" do + result = Worker.work do + 3 + 4 + end + result.should == 7 + end + + it "executes a block in the context of the calling method" do + n = 1 + result = Worker.work do + n + 4 + end + result.should == 5 + end + + + it "executes a block 3 times and returns the result" do + n = 5 + result = Worker.work(3) do + n += 1 + end + result.should == 8 + end + +end