Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions week4/class_materials/odd_number.rb
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions week4/class_materials/resources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Blocks, Procs, Lambdas, and Closures: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/


8 changes: 8 additions & 0 deletions week4/class_materials/timer.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions week4/class_materials/timer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file added week4/class_materials/week4.key
Binary file not shown.
Binary file added week4/class_materials/week4.pdf
Binary file not shown.
53 changes: 53 additions & 0 deletions week4/exercises/monsters.rb
Original file line number Diff line number Diff line change
@@ -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]}

13 changes: 13 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -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?
37 changes: 37 additions & 0 deletions week4/homework/worker_spec.rb
Original file line number Diff line number Diff line change
@@ -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