Skip to content

Commit

Permalink
rename chX to sessionX
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Jun 11, 2011
1 parent 2709901 commit eadc31e
Show file tree
Hide file tree
Showing 277 changed files with 137 additions and 96 deletions.
72 changes: 36 additions & 36 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ module Rake
def display_tasks_and_comments
displayable_tasks = tasks.select { |t| t.comment && t.name =~ options.show_task_pattern }.sort do
|t1,t2|
one_is_ch = t1.name[/^\d+:(\d+|all)$/]
two_is_ch = t2.name[/^\d+:(\d+|all)$/]
if one_is_ch && two_is_ch
ch1 , prb1 = t1.name.split ':'
ch2 , prb2 = t2.name.split ':'
if ch1 == ch2
one_is_session = t1.name[/^\d+:(\d+|all)$/]
two_is_session = t2.name[/^\d+:(\d+|all)$/]
if one_is_session && two_is_session
session1 , prb1 = t1.name.split ':'
session2 , prb2 = t2.name.split ':'
if session1 == session2
if prb1 == 'all' then 1 elsif prb2 == 'all' then -1 else prb1.to_i <=> prb2.to_i end
else
ch1.to_i <=> ch2.to_i
session1.to_i <=> session2.to_i
end
elsif one_is_ch
elsif one_is_session
-1
elsif two_is_ch
elsif two_is_session
1
else
t1.name <=> t2.name
Expand Down Expand Up @@ -56,54 +56,54 @@ end
# runs the spec against the challenge
# if the environment variable 'solved' has been set,
# then it runs them against the solved challenge instead
def run_spec(chapter,problem)
ch = "#{File.dirname(__FILE__)}/ch#{chapter}"
def run_spec(session_num,problem)
sess = "#{File.dirname(__FILE__)}/session#{session_num}"
if ENV['solved']
problem_dir = "#{ch}/solved/#{problem}.rb"
problem_dir = "#{sess}/solved/#{problem}.rb"
else
problem_dir = Dir["#{ch}/challenge/#{problem}_*"].first
problem_dir = Dir["#{sess}/challenge/#{problem}_*"].first
end
sh "ruby -c '#{problem_dir}'" # check syntax
sh "rspec -cr '#{problem_dir}' -r enumerator '#{ch}/spec/#{problem}.rb' --fail-fast" # run spec (c for colour, r to require the files, enumerator required for 1.8.6 compatibility)
sh "ruby -c '#{problem_dir}'" # check syntax
sh "rspec -cr '#{problem_dir}' -r enumerator '#{sess}/spec/#{problem}.rb' --fail-fast" # run spec (c for colour, r to require the files, enumerator required for 1.8.6 compatibility)
end


# returns array of chapters
def get_chapters
Dir['*'].grep(/^ch\d+$/).map { |folder_name| folder_name[/\d+$/] }
# returns array of sessions
def get_sessions
Dir['*'].grep(/^session\d+$/).map { |folder_name| folder_name[/\d+$/] }
end


# returns hash where each key is the chapter to test
# each value is a range indicating the problems available for this chapter
def chapters_with_problems
get_chapters.inject Hash.new do |chapters_with_sections,chapter|
challenges = Dir["ch#{chapter}/challenge/*"]
chapters_with_sections[chapter] = challenges.map { |challenge| challenge[%r([^/]+?$)].to_i }.sort
chapters_with_sections
# returns hash where each key is the session to test
# each value is a range indicating the problems available for this session
def sessions_with_problems
get_sessions.inject Hash.new do |sessions_with_sections, session|
challenges = Dir["session#{session}/challenge/*"]
sessions_with_sections[session] = challenges.map { |challenge| challenge[%r([^/]+?$)].to_i }.sort
sessions_with_sections
end
end


# create tasks for each challenge
chapters_with_problems.each do |chapter,problems|
namespace chapter do
sessions_with_problems.each do |session, problems|
namespace session do
problems.each do |problem|
desc "Test chapter#{chapter} problem#{problem}"
task(problem) { run_spec chapter , problem }
desc "Test session#{session} problem#{problem}"
task(problem) { run_spec session , problem }
end
desc "Test chapter#{chapter} all problems"
desc "Test session#{session} all problems"
task :all => problems.to_a
end
end

task :default => get_chapters.map { |chapter| "#{chapter}:all" }
task :default => get_sessions.map { |session| "#{session}:all" }



desc 'create chapter skeleton'
task :add_ch do
ch = "ch#{ENV['ch']||get_chapters.size.next}"
makedirs 'challenge' , 'examples' , 'notes' , 'solved' , 'spec' , :path => ch
touch "#{ch}/notes/josh.rb"
desc 'create session skeleton'
task :add_sess do
sess = "session#{ENV['sess']||get_sessions.size.next}"
makedirs 'challenge' , 'examples' , 'notes' , 'solved' , 'spec' , :path => sess
touch "#{sess}/notes/josh.rb"
end
6 changes: 0 additions & 6 deletions ch5/examples/5_calculator/calculator.rb

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions session5/examples/5_calculator
Submodule 5_calculator added at 4d6916
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
154 changes: 100 additions & 54 deletions ch5/notes/notes.rb → session5/notes/notes.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,114 @@
# ===== Exceptions =====
# errors and exceptions are used to say "something fucked up"
# it halts evaluation, and returns to where it was called from
# incrementally making its way up the stack (the series of methods that were called to get to
# wherever the exception was raised) until it is either rescued, or crashes the program

# There are quite a few exceptions in Ruby (one of your challenges will be to find them :)
# You've seen them before, but perhaps didn't know what they were
def method_with_one_parameter(n) end
begin
method_with_one_parameter(1,2,3)
1 + 2 # =>
rescue => e
e # => #<ArgumentError: wrong number of arguments (3 for 1)>
e.class # => ArgumentError
e.class.ancestors # => [ArgumentError, StandardError, Exception, Object, Kernel]
end
# 01234567890123456789012345678901234567890123456789012345678901234567

# You can rescue specific exceptions if you like
begin
method_with_one_parameter(1,2,3)
rescue ZeroDivisionError => e
e # =>
rescue ArgumentError => e
e # => #<ArgumentError: wrong number of arguments (3 for 1)>
# ===== Modules =====
# Generally, inheriting classes is becoming less embraced by the Ruby community
# Now, people usually prefer to use modules instead of inheritence.
# A class can only inherit from one other class, but it can include as many modules as it likes (this is commonly called a mix-in)
#
# Modules have the feel of a class in that they have methods on them. But they cannot be instantiated.
# Instead, you declare that the methods in the module should extend an object, or be included in a class
# and all the methods the module contains will be magically dropped into the class.
module Threes

def threes_r0
self.select { |i| i % 3 == 0 }
end

def threes_r1
select { |i| i % 3 == 1 }
end

def threes_r2
select { |i| i % 3 == 2 }
end

end

begin
1 / 0
rescue ZeroDivisionError => e
e # => #<ZeroDivisionError: divided by 0>
rescue ArgumentError => e
e # =>
end

# If you handle the exception, you may want to try again
numerator = 100
denominator = 0
begin
result = numerator / denominator
rescue ZeroDivisionError => e
denominator += 1
retry
# Extending an object with a module
sequence = 0...30
sequence.extend Threes


# if we look in sequence's singleton class, we see that Threes is now an ancestor of it
# but this has only affected our one object
(class << sequence; self; end).ancestors # =>
(class << Range.new(0,0); self; end).ancestors # =>

sequence.threes_r0 # =>
sequence.threes_r1 # =>
sequence.threes_r2 # =>

sequence # =>
0...30 # =>
sequence.methods.grep(/threes/) # =>
(0...30).methods.grep(/threes/) # =>

# Questions: Where did the select come from in threes_rn?
# Can you think of another object we could extend with this module?


# Including a module in a class' instance methods
# A class has lots of instance methods that its instances are able to use
# You can include the module's methods in them as well
Range.ancestors # =>
class Range
include Threes
end
result # => 100
denominator # => 1

# You can raise your own exceptions
def show_exception
begin
yield
rescue Exception => e
e
Range.ancestors # =>

sequence # =>
0...30 # =>
sequence.methods.grep(/threes/) # =>
(0...30).methods.grep(/threes/) # =>



# Modules are also commonly used for namespacing
# maybe you want to try the same problem several days in a row, to see how your approach changes
module Day1Solutions
class MinFinder
def initialize(a,b)
@a , @b = a , b
end
def solve
if @a < @b then @a else @b end
end
end
end

def get_names(full_name)
raise ArgumentError.new("you need to submit the name as 'Firstname Lastname'") unless full_name[ /^[A-Z][a-z]* [A-Z][a-z]*$/ ]
full_name.split
module Day2Solutions
class MinFinder
def initialize(a,b)
@elements = [ a , b ]
end
def solve
@elements.min
end
end
end

show_exception { get_names 'josh cheek' } # => #<ArgumentError: you need to submit the name as 'Firstname Lastname'>
show_exception { get_names 'Josh Cheek' } # => ["Josh", "Cheek"]
day1 = Day1Solutions::MinFinder.new 10 , 5
day2 = Day2Solutions::MinFinder.new 10 , 5
day1 # =>
day2 # =>
day1.solve # =>
day2.solve # =>

# that is a lot to type, though, I think I like Day2Solutions better, it's easier to read
# that is the one I want to use from now on, but I don't want to have to keep typing Day2Solutions::MinFinder.new
# we can include day2 solutions into our main
include Day2Solutions
MinFinder.new 10 , 5 # =>

# This is a common way to get nicer functionality
# For example, the FileUtils module (http://ruby-doc.org/core/classes/FileUtils.html), which is in the Ruby standard library
# is included when working with rake files, this lets you say things like "cd 'ch4'" instead of "FileUtils.cd 'ch4'"

# ~> -:264:in `lkjlkj': super: no superclass method `lkjlkj' for #<Engineer:0x0000010090ba40 @hours_worked=10> (NoMethodError)
# ~> from -:268:in `<main>'


# QUESTION: Why did our program return an ArgumentError? We caught an Exception



Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit eadc31e

Please sign in to comment.