Skip to content
Browse files

Add day1 examples and Thor/Bundler setup

  • Loading branch information...
1 parent 3f83f18 commit 8694ec0c8e1ae95c3031ca91cbd96ec9f8cc4517 @mbbx6spp mbbx6spp committed Jun 14, 2011
Showing with 249 additions and 0 deletions.
  1. +5 −0 ruby/Gemfile
  2. +45 −0 ruby/Gemfile.lock
  3. +21 −0 ruby/Thorfile
  4. +2 −0 ruby/day1/features/support/env.rb
  5. +57 −0 ruby/day1/guesser.rb
  6. +27 −0 ruby/day1/selfstudy.rb
  7. +90 −0 ruby/day1/selfstudy_spec.rb
  8. +2 −0 ruby/day1/standalone.rb
View
5 ruby/Gemfile
@@ -0,0 +1,5 @@
+source 'http://rubygems.org'
+
+gem 'thor'
+gem 'rspec'
+gem 'aruba'
View
45 ruby/Gemfile.lock
@@ -0,0 +1,45 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ aruba (0.4.1)
+ bcat (>= 0.6.1)
+ childprocess (>= 0.1.9)
+ cucumber (>= 0.10.7)
+ rdiscount (>= 1.6.8)
+ rspec (>= 2.6.0)
+ bcat (0.6.1)
+ rack (~> 1.0)
+ builder (3.0.0)
+ childprocess (0.1.9)
+ ffi (~> 1.0.6)
+ cucumber (0.10.7)
+ builder (>= 2.1.2)
+ diff-lcs (>= 1.1.2)
+ gherkin (~> 2.4.0)
+ json (>= 1.4.6)
+ term-ansicolor (>= 1.0.5)
+ diff-lcs (1.1.2)
+ ffi (1.0.9)
+ gherkin (2.4.0)
+ json (>= 1.4.6)
+ json (1.5.2)
+ rack (1.3.0)
+ rdiscount (1.6.8)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.0)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ term-ansicolor (1.0.5)
+ thor (0.14.6)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ aruba
+ rspec
+ thor
View
21 ruby/Thorfile
@@ -0,0 +1,21 @@
+require 'rubygems'
+require 'thor'
+
+class SevenWeeks < Thor
+ desc "examples_for DAY", "Run examples for specific day"
+ method_options :count => :integer
+ def examples_for(day)
+ dir = "day#{day}"
+ if File.exists?(dir) && File.directory?(dir)
+ file = File.join(dir, "selfstudy_spec.rb")
+ if File.exists?(file)
+ output = %x[rspec #{file}]
+ puts output
+ else
+ $stderr.puts "ERROR: Self study examples not found in day #{day} directory"
+ end
+ else
+ $stderr.puts "ERROR: Day #{day} examples not found"
+ end
+ end
+end
View
2 ruby/day1/features/support/env.rb
@@ -0,0 +1,2 @@
+require 'aruba'
+
View
57 ruby/day1/guesser.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/env ruby
+
+def guesser
+ def init
+ # Must avoid answer being 0 otherwise "k".to_i => 0 would work!
+ answer = rand(9) + 1
+ puts answer
+ ask
+ guess = until_within_range(gets.to_i)
+ puts guess
+ counter = 1
+ [guess, answer, counter]
+ end
+
+ def range?(user_guess)
+ (0 < user_guess) && (user_guess <= 10)
+ end
+
+ def correct?(user_guess, actual_answer)
+ (user_guess === actual_answer)
+ end
+
+ def ask
+ puts "What number (1-10) would you like to guess?"
+ end
+
+ def out_of_range
+ $stderr.puts "ERROR: Guess out of range (1-10). Try again: "
+ end
+
+ # Yeah, recursion is not a great idea in languages like Ruby, but...
+ # since the recursion depth here is not expected to be high it should be fine
+ def until_within_range(guess)
+ if not range?(guess)
+ out_of_range
+ until_within_range(gets.to_i)
+ else
+ return guess
+ end
+ end
+
+ # Initial setup and question/answer
+ (guess, answer, counter) = init
+ puts guess, answer, counter
+
+ # subsequent guesses/checks
+ while (not correct?(guess, answer))
+ ask
+ guess = until_within_range(gets.to_i)
+ counter += 1
+ end
+
+ # Must be correct or something went wrong!?:)
+ puts "Correct! Wooohooo! You guessed in #{counter} tries."
+end
+
+guesser
View
27 ruby/day1/selfstudy.rb
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+
+# Woot woot - thinking like a n000b
+
+puts "Hello, world."
+# but I could also do:
+puts ".dlrow ,olleH".reverse
+# or even
+["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "."].each do |c|
+ print c
+end
+puts
+
+s = "Hello, Ruby."
+index = s.index("Ruby")
+puts "The index of the word 'Ruby' is: #{index}"
+
+10.times { puts "Susan Potter sucks, but not as much as Tony Blair!" }
+
+(1..10).each do |i|
+ puts "This is sentence number #{i}"
+end
+
+# Run a Ruby program from a file...
+puts "To run a Ruby program from a file do: ruby <filename>"
+
+# see spec examples for rest from now on.
View
90 ruby/day1/selfstudy_spec.rb
@@ -0,0 +1,90 @@
+#!/usr/bin/env rspec
+
+require 'rubygems'
+require 'rspec'
+
+RSpec.configure do |config|
+ config.mock_with :rspec
+end
+
+# Of course, these are kind of contrived since I am depending on
+# implementation details (a little). However, the self-study exercises
+# are all a little contrived too!:)
+describe "Day One, Week One: Self-Study Exercises" do
+ describe "Print the string 'Hello, world.'" do
+ before(:all) do
+ @output = "Hello, world."
+ end
+ it "should pass 'Hello, world.' as an argument to puts on $stdout object" do
+ $stdout.should_receive(:puts).with(@output)
+ puts @output
+ end
+ end
+
+ describe "Find index of 'Ruby' in string 'Hello, Ruby'" do
+ before(:all) do
+ @string = "Hello, Ruby"
+ end
+
+ it "should return 7 as index of 'Ruby'" do
+ @string.index("Ruby").should === 7
+ end
+ end
+
+ describe "Print name ten times" do
+ before(:all) do
+ @name = "Florence And The Machine" # a much better name than Susan Potter
+ end
+
+ it "should pass name to puts on $stdout object ten times" do
+ $stdout.should_receive(:puts).with(@name).exactly(10).times
+ 10.times { puts @name }
+ end
+ end
+
+ describe "Print string 'This is sentence number X' where X is integer starting at 1 incrementing by 1 to 10" do
+ before(:all) do
+ @base = "This is sentence number"
+ @start = 1
+ @end = 10
+ end
+
+ def do_it
+ (1..10).each do |i|
+ puts [@base, i].join(" ")
+ end
+ end
+
+ it "should pass base string to puts on $stdout object ten times" do
+ $stdout.should_receive(:puts).with(Regexp.new(@base)).exactly(10).times
+ do_it
+ end
+
+ it "should increment counter each time by one starting at one ten times" do
+ (1..10).each do |index|
+ $stdout.should_receive(:puts).once.with(@base + " " + index.to_s).ordered
+ end
+ do_it
+ end
+ end
+
+ describe "Run a Ruby program from a file" do
+ before(:each) do
+ @filename = File.join(File.dirname(__FILE__), "standalone.rb")
+ @expected_output = "This is a standalone Ruby script sucka!"
+ end
+
+ def do_it
+ system("ruby #{@filename}")
+ end
+
+ it "should print to $stdout expected output" do
+ $stdout.should_receive(:puts).with(@expected_output)
+ end
+
+ it "should have a return status of 0" do
+ ret_value = do_it
+ ret_value.should be true
+ end
+ end
+end
View
2 ruby/day1/standalone.rb
@@ -0,0 +1,2 @@
+
+puts "This is a standalone Ruby script sucka!"

0 comments on commit 8694ec0

Please sign in to comment.
Something went wrong with that request. Please try again.