From 64f0f8efcfcda8347bc4d7eaf34d665549d5d22c Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Mon, 15 Oct 2012 23:07:17 -0700 Subject: [PATCH 01/22] Week1 Homework --- .gitignore | 1 + week1/exercises/rspec_spec.rb | 6 ++++-- week1/homework/questions.txt | 13 +++++++++++++ week1/homework/strings_and_rspec_spec.rb | 12 ++++++------ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index fb28001..b9b7ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ spec/reports test/tmp test/version_tmp tmp +.idea # YARD artifacts .yardoc diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index f4c2f0b..b1c20ae 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,11 +43,13 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 1.should eq 1 end - it "supports placeholder examples that lack code (like this one)" + it "supports placeholder examples that lack code (like this one)" do + "true".should eq "true" + end it "requires that examples use expectations (like #should) to work properly" do diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index cd5ff26..43c43dc 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,8 +3,21 @@ Chapter 3 Classes, Objects, and Variables p.90-94 Strings 1. What is an object? + Anything that is manipulated in Ruby + 2. What is a variable? + A reference to an object + 3. What is the difference between an object and a class? + classes create all objects + 4. What is a String? + It's a sequence of characters + 5. What are three messages that I can send to a string object? Hint: think methods + .chomp + .split + .squeeze + 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + with single quotes(') or double quotes("). There are many more escape sequences and code substitution with " diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 0a8354e..d9df149 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,15 +12,15 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" - it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + it "should be able to count the characters" do + @my_string.length.should eq 66 + end + it "should be able to split on the . character" do + result = @my_string.split('.') result.should have(2).items end it "should be able to give the encoding of the string" do - pending - #should eq (Encoding.find("UTF-8")) + @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end From ae0ff6a0aa6813311c6a27fd88e742f3699c7d50 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Fri, 19 Oct 2012 14:59:13 -0700 Subject: [PATCH 02/22] book work --- week2/exercises/book.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index e05e468..717834d 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,2 +1,10 @@ class Book + attr_accessor :title + def initialize(title, page_count) + @title = title + @page_count = page_count + end + def page_count + "Page count is " + @page_count.to_s + end end From fd3ea6aa9f5cc38edae2decc4e90b7f31b8c5f73 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Mon, 22 Oct 2012 23:32:28 -0700 Subject: [PATCH 03/22] Week 2 Homework --- week2/homework/questions.txt | 13 +++++++++++-- week2/homework/simon_says.rb | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 247547d..8971966 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,7 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + And Array is indexed with positive integers and a hash is a key value pair indexed by the key object 2. When would you use an Array over a Hash and vice versa? -3. What is a module? Enumerable is a built in Ruby module, what is it? -4. Can you inherit more than one thing in Ruby? How could you get around this problem? + You'd want to use arrays when you need to iterate over the integer index. Hashes are better suited for storing and looking objects up with a key +3. What is a module? + A module is a set of code that is separated by a namespace. Useful to avoid naming clashes +Enumerable is a built in Ruby module, what is it? + It's a mixin that defines many methods for iteration +4. Can you inherit more than one thing in Ruby? + No, you can only inherit from one base class +How could you get around this problem? + You can include Mixins 5. What is the difference between a Module and a Class? + A module is a namespace and isn't instantiated. It can define classes within it however. A class is instantiated \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..c2f9e9d --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + def echo(textToEcho) + textToEcho + end + + def shout(textToShout) + textToShout.upcase + end + + def repeat(textToRepeat, count=2) + Array.new(count, textToRepeat + " ").inject(:+).chomp(" ") + end + + def start_of_word(word, count) + word[0...count] + end + + def first_word(sentence) + sentence.scan(/[\w']+/)[0] + end +end \ No newline at end of file From 00c4fe87f2d0b4edfb917585a993866bc84d2ad6 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sun, 28 Oct 2012 23:48:27 -0700 Subject: [PATCH 04/22] calculator tests and methods --- week3/homework/calculator.rb | 28 ++++++++++++++++++++++++ week3/homework/calculator_spec.rb | 36 ++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index ede464b..0012e54 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,2 +1,30 @@ class Calculator + def sum(numberArray) + if (numberArray.count == 0) + return 0 + end + numberArray.inject(:+) + end + + def multiply2numbers(firstNumber, secondNumber) + firstNumber * secondNumber + end + + def multiply(numberArray) + if (numberArray.count == 0) + return 0 + end + numberArray.inject(:*) + end + + def power(baseNumber, exponent) + Array.new(exponent, baseNumber).inject(:*) + end + + def factorial(number) + if (number == 0) + return 0 + end + Math.gamma(number + 1) + end end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 08b81cb..adc1103 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -27,19 +27,39 @@ # Once the above tests pass, # write tests and code for the following: - it "multiplies two numbers" + it "multiplies two numbers" do + @calculator.multiply2numbers(2,2).should == 4 + end - it "multiplies an array of numbers" + it "multiplies an array of numbers" do + @calculator.multiply([2,2,6]).should == 24 + end - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + @calculator.power(4,2).should == 16 + end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + @calculator.factorial(0).should == 0 + end + + it "computes the factorial of 1" do + @calculator.factorial(1).should == 1 + end + + it "computes the factorial of 2" do + @calculator.factorial(2).should == 2 + end + + it "computes the factorial of 5" do + @calculator.factorial(5).should == 120 + end + + it "computes the factorial of 10" do + @calculator.factorial(10).should == 3628800 + end end end From 03d989356ee7692750c7e433c82176e22f3bc0c3 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Mon, 29 Oct 2012 23:12:33 -0700 Subject: [PATCH 05/22] Finished Week 3 Homework --- week3/homework/calculator.rb | 17 ++++------------- week3/homework/questions.txt | 5 +++++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index 0012e54..c963106 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,9 +1,6 @@ class Calculator def sum(numberArray) - if (numberArray.count == 0) - return 0 - end - numberArray.inject(:+) + numberArray.count == 0 ? 0 : numberArray.inject(:+) end def multiply2numbers(firstNumber, secondNumber) @@ -11,20 +8,14 @@ def multiply2numbers(firstNumber, secondNumber) end def multiply(numberArray) - if (numberArray.count == 0) - return 0 - end - numberArray.inject(:*) + numberArray.count == 0 ? 0 : numberArray.inject(:*) end def power(baseNumber, exponent) - Array.new(exponent, baseNumber).inject(:*) + baseNumber**exponent end def factorial(number) - if (number == 0) - return 0 - end - Math.gamma(number + 1) + number == 0 ? 0 : Math.gamma(number + 1) end end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index e9a9079..555bdc1 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,8 +5,13 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + A symbol is an identifier that doesn't change in memory 2. What is the difference between a symbol and a string? + Two identical strings will create new instances in memory whereas two symbols will have one 3. What is a block and how do I call a block? + A block is a chunk of code between {} or do end. It's called as a argument to a method or yielded inside a method 4. How do I pass a block to a method? What is the method signature? + It's passed as an argument after the method call: doSomething {puts 'Block'} 5. Where would you use regular expressions? + Many places. It's used for string matching, modification and replacement From 05d7e50de15c8a94a6643817d57d499a3a449976 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Tue, 30 Oct 2012 18:15:17 -0700 Subject: [PATCH 06/22] improvements --- week3/homework/calculator.rb | 10 +++------- week3/homework/calculator_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index e250459..d1c2425 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -3,12 +3,8 @@ def sum(numberArray) numberArray.count == 0 ? 0 : numberArray.inject(:+) end - def multiply2numbers(firstNumber, secondNumber) - firstNumber * secondNumber - end - - def multiply(numberArray) - numberArray.count == 0 ? 0 : numberArray.inject(:*) + def multiply(*numberArray) + numberArray.flatten().count == 0 ? 0 : numberArray.flatten().inject(:*) end def power(baseNumber, exponent) @@ -16,6 +12,6 @@ def power(baseNumber, exponent) end def factorial(number) - number == 0 ? 0 : Math.gamma(number + 1) + number == 0 ? 1 : Math.gamma(number + 1) end end diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 97508a4..936b7f8 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -27,7 +27,7 @@ # Once the above tests pass, # write tests and code for the following: it "multiplies two numbers" do - @calculator.multiply2numbers(2,2).should == 4 + @calculator.multiply(2,2).should == 4 end it "multiplies an array of numbers" do @@ -41,7 +41,7 @@ # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do it "computes the factorial of 0" do - @calculator.factorial(0).should == 0 + @calculator.factorial(0).should == 1 end it "computes the factorial of 1" do From 0801c75fb4f55f64c54d7037bd07881dc4ca8a49 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Mon, 5 Nov 2012 20:38:21 -0800 Subject: [PATCH 07/22] class exercises --- week4/class_materials/timer.rb | 8 ++++++++ week4/class_materials/timer_spec.rb | 11 +++++++++++ week4/exercises/worker.rb | 7 +++++++ 3 files changed, 26 insertions(+) create mode 100644 week4/class_materials/timer.rb create mode 100644 week4/exercises/worker.rb diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb new file mode 100644 index 0000000..1e48bc2 --- /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 index a6092f1..ba59ff5 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -17,4 +17,15 @@ flag.should be_true end + it "should run our code multiple times" do + counter = 0 + result = Timer.time_code(17){counter += 1} + counter.should eq 17 + end + + it "should give the average time" do + Time.stub(:now).and_return(0,1) + result = Timer.time_code(10) { } + result.should equal 10 + end end diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..9161023 --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,7 @@ +class Worker + def self.work(n=1) + result = nil + n.times{result = yield} + result + end +end \ No newline at end of file From 1f92b44d260e38dc517199f73ff6790781a7bd6c Mon Sep 17 00:00:00 2001 From: scurtiss Date: Tue, 6 Nov 2012 16:11:12 -0800 Subject: [PATCH 08/22] Update week4/homework/questions.txt --- week4/homework/questions.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..3617124 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,14 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + It reads files using the objects in the IO class. Specifically the File subclass. (File.open(...)) 2. How would you output "Hello World!" to a file called my_output.txt? + file = File.new("my_output.txt", "w") + file.puts("Hello World!") + file.close 3. What is the Directory class and what is it used for? + The Directory class is used for handling file system directories. creating, deleting, etc... 4. What is an IO object? + An IO object is an object that encompasses all input and output related tasks such as reading from the command line and outputting to a file to name a couple examples. 5. What is rake and what is it used for? What is a rake task? + Rake stands for Ruby Make which is used to do build and deployment tasks. \ No newline at end of file From 55df5cfbda19af9058f478b83be874a890940600 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sat, 10 Nov 2012 20:22:21 -0800 Subject: [PATCH 09/22] exercises --- week5/exercises/Rakefile | 24 ++++++++++++++++++++++++ week5/exercises/skier.rb | 11 +++++++++++ 2 files changed, 35 insertions(+) create mode 100644 week5/exercises/skier.rb diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index 68a8f60..e13f059 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -1,3 +1,27 @@ task :test do puts "Hello World!" end + +task :names do + getNamesFromFile.each do |name| + puts name + end +end + +task :createclassdir do + Dir.mkdir(:RubyClass) unless Dir.exists?(:RubyClass) +end + +task :createnamedir => [:createclassdir] do + +end + +def getNamesFromFile(file="names") + store_names = [] + f = File.open("names", "r") + f.each do |name| + store_names << name + end + f.close + return store_names +end \ No newline at end of file diff --git a/week5/exercises/skier.rb b/week5/exercises/skier.rb new file mode 100644 index 0000000..e5ac76c --- /dev/null +++ b/week5/exercises/skier.rb @@ -0,0 +1,11 @@ +class Skier + attr_accessor :events + + def initialize(events=[:gs, :downhill]) + @events = events + end + + def ski(event) + put "This skier is skiing #{event}" + end +end \ No newline at end of file From 411833889f66bfc1bd706da35b41ab161779eae8 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Tue, 13 Nov 2012 19:42:57 -0800 Subject: [PATCH 10/22] testgem --- week6/hologem/bin/hologem | 0 week6/hologem/hologem.gemspec | 12 ++++++++++++ week6/hologem/lib/hologem.rb | 1 + week6/hologem/spec/Rakefile | 5 +++++ week6/hologem/spec/hologem_spec.rb | 1 + 5 files changed, 19 insertions(+) create mode 100644 week6/hologem/bin/hologem create mode 100644 week6/hologem/hologem.gemspec create mode 100644 week6/hologem/lib/hologem.rb create mode 100644 week6/hologem/spec/Rakefile create mode 100644 week6/hologem/spec/hologem_spec.rb diff --git a/week6/hologem/bin/hologem b/week6/hologem/bin/hologem new file mode 100644 index 0000000..e69de29 diff --git a/week6/hologem/hologem.gemspec b/week6/hologem/hologem.gemspec new file mode 100644 index 0000000..7d54afd --- /dev/null +++ b/week6/hologem/hologem.gemspec @@ -0,0 +1,12 @@ +Gem::Specification.new do |s| + s.name = 'hologem' + s.version = '0.0.2' + s.date = '2012-11-13' + s.summary = "Tutorial Gem" + s.description = "A gem to learn how to make gems" + s.authors = ["Steven Curtiss"] + s.email = 'stevencurtiss@hotmail.com' + s.homepage = 'http://rubygems.org/gems/hologem' + s.files = ["lib/hologem.rb"] + s.executables << "hologem" +end \ No newline at end of file diff --git a/week6/hologem/lib/hologem.rb b/week6/hologem/lib/hologem.rb new file mode 100644 index 0000000..7f09b2f --- /dev/null +++ b/week6/hologem/lib/hologem.rb @@ -0,0 +1 @@ +puts "Hello World!" \ No newline at end of file diff --git a/week6/hologem/spec/Rakefile b/week6/hologem/spec/Rakefile new file mode 100644 index 0000000..cc1ffb1 --- /dev/null +++ b/week6/hologem/spec/Rakefile @@ -0,0 +1,5 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new('test') + +task :default => :test diff --git a/week6/hologem/spec/hologem_spec.rb b/week6/hologem/spec/hologem_spec.rb new file mode 100644 index 0000000..2df27ca --- /dev/null +++ b/week6/hologem/spec/hologem_spec.rb @@ -0,0 +1 @@ +require 'hologem' \ No newline at end of file From 659e9842b1a938795ff07d72015a226307bc8671 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Tue, 13 Nov 2012 20:09:43 -0800 Subject: [PATCH 11/22] spaz_gem --- week6/hologem/.rspec | 2 ++ week6/hologem/{spec => }/Rakefile | 0 week6/hologem/bin/hologem | 4 ++++ week6/hologem/lib/hologem.rb | 4 +++- week6/hologem/spec/hologem_spec.rb | 10 +++++++++- week6/spaz_gem | 1 + 6 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 week6/hologem/.rspec rename week6/hologem/{spec => }/Rakefile (100%) create mode 160000 week6/spaz_gem diff --git a/week6/hologem/.rspec b/week6/hologem/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week6/hologem/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week6/hologem/spec/Rakefile b/week6/hologem/Rakefile similarity index 100% rename from week6/hologem/spec/Rakefile rename to week6/hologem/Rakefile diff --git a/week6/hologem/bin/hologem b/week6/hologem/bin/hologem index e69de29..044c5e3 100644 --- a/week6/hologem/bin/hologem +++ b/week6/hologem/bin/hologem @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +puts "Hello There!" +puts ARGV[0] \ No newline at end of file diff --git a/week6/hologem/lib/hologem.rb b/week6/hologem/lib/hologem.rb index 7f09b2f..1bd5f23 100644 --- a/week6/hologem/lib/hologem.rb +++ b/week6/hologem/lib/hologem.rb @@ -1 +1,3 @@ -puts "Hello World!" \ No newline at end of file +puts "Hello World!" +class Hello +end \ No newline at end of file diff --git a/week6/hologem/spec/hologem_spec.rb b/week6/hologem/spec/hologem_spec.rb index 2df27ca..12831d5 100644 --- a/week6/hologem/spec/hologem_spec.rb +++ b/week6/hologem/spec/hologem_spec.rb @@ -1 +1,9 @@ -require 'hologem' \ No newline at end of file +require 'hologem' + +describe "hologem" do + it "Should make a new Hello" do + h = Hello.new + h.should be_a Hello + end + +end \ No newline at end of file diff --git a/week6/spaz_gem b/week6/spaz_gem new file mode 160000 index 0000000..47541d4 --- /dev/null +++ b/week6/spaz_gem @@ -0,0 +1 @@ +Subproject commit 47541d400059036bf97b7415d668a5ee4c5194b3 From 23cf240203d0b97fd0bd2806e378b8c3891e104c Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sat, 24 Nov 2012 17:25:50 -0800 Subject: [PATCH 12/22] class exercises --- .../features/converter.feature | 0 .../features/step_definitions/converter.rb | 21 ++++++++ .../step_definitions/converter_steps.rb | 51 +++++++++++++++++++ .../{ => puppy}/features/puppy.feature | 0 .../puppy/features/step_definitions/puppy.rb | 0 .../features/step_definitions/puppy_steps.rb | 0 6 files changed, 72 insertions(+) rename week7/exercises/{ => converter}/features/converter.feature (100%) create mode 100644 week7/exercises/converter/features/step_definitions/converter.rb create mode 100644 week7/exercises/converter/features/step_definitions/converter_steps.rb rename week7/exercises/{ => puppy}/features/puppy.feature (100%) create mode 100644 week7/exercises/puppy/features/step_definitions/puppy.rb create mode 100644 week7/exercises/puppy/features/step_definitions/puppy_steps.rb diff --git a/week7/exercises/features/converter.feature b/week7/exercises/converter/features/converter.feature similarity index 100% rename from week7/exercises/features/converter.feature rename to week7/exercises/converter/features/converter.feature diff --git a/week7/exercises/converter/features/step_definitions/converter.rb b/week7/exercises/converter/features/step_definitions/converter.rb new file mode 100644 index 0000000..25849d7 --- /dev/null +++ b/week7/exercises/converter/features/step_definitions/converter.rb @@ -0,0 +1,21 @@ +class Converter + def initialize(value) + @value = value.to_f + end + + def type=(type) + @type = type + end + + def convert + self.send("#{@type}_conversion") + end + + def Celsius_conversion + (@value * (9.0/5.0) + 32.0).round(1) + end + + def Fahrenheit_conversion + ((@value - 32) * (5.0/9.0)).round(1) + end +end \ No newline at end of file diff --git a/week7/exercises/converter/features/step_definitions/converter_steps.rb b/week7/exercises/converter/features/step_definitions/converter_steps.rb new file mode 100644 index 0000000..0385ce6 --- /dev/null +++ b/week7/exercises/converter/features/step_definitions/converter_steps.rb @@ -0,0 +1,51 @@ +Given /^I have entered (\d+) into the converter$/ do |arg1| + @converter = Converter.new(arg1) +end + +Given /^I set the type to Fahrenheit$/ do + @converter.type = "Fahrenheit" +end + +When /^I press convert$/ do + @result = @converter.convert +end + +Then /^the result returned should be (\d+)\.(\d+)$/ do |arg1, arg2| + @result.should eq "#{arg1}.#{arg2}".to_f +end + +Given /^we have a puppy$/ do + @puppy = Puppy.new() +end + +Given /^its name is (\w)$/ do |arg1| + @puppy.name = arg1 +end + +When /^we pet the puppy$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^the puppy wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +Given /^its name is Bella$/ do + pending # express the regexp above with the code you wish you had +end + +When /^we ring the bell$/ do + pending # express the regexp above with the code you wish you had +end + +When /^it wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^we must take it out$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^then it will not pee on the floor$/ do + pending # express the regexp above with the code you wish you had +end diff --git a/week7/exercises/features/puppy.feature b/week7/exercises/puppy/features/puppy.feature similarity index 100% rename from week7/exercises/features/puppy.feature rename to week7/exercises/puppy/features/puppy.feature diff --git a/week7/exercises/puppy/features/step_definitions/puppy.rb b/week7/exercises/puppy/features/step_definitions/puppy.rb new file mode 100644 index 0000000..e69de29 diff --git a/week7/exercises/puppy/features/step_definitions/puppy_steps.rb b/week7/exercises/puppy/features/step_definitions/puppy_steps.rb new file mode 100644 index 0000000..e69de29 From 69033dc8e4508b7bf9468ce78405028b1940f0f0 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sat, 24 Nov 2012 17:26:42 -0800 Subject: [PATCH 13/22] ? --- week6/spaz_gem | 1 - 1 file changed, 1 deletion(-) delete mode 160000 week6/spaz_gem diff --git a/week6/spaz_gem b/week6/spaz_gem deleted file mode 160000 index 47541d4..0000000 --- a/week6/spaz_gem +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 47541d400059036bf97b7415d668a5ee4c5194b3 From a6fcd9bb21a8388ade84ccad994c69efb2b75113 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sat, 24 Nov 2012 17:27:27 -0800 Subject: [PATCH 14/22] ?? --- .../step_definitions/converter_steps.rb | 35 ------------------- .../puppy/features/step_definitions/puppy.rb | 8 +++++ .../features/step_definitions/puppy_steps.rb | 31 ++++++++++++++++ 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/week7/exercises/converter/features/step_definitions/converter_steps.rb b/week7/exercises/converter/features/step_definitions/converter_steps.rb index 0385ce6..1dc9ed2 100644 --- a/week7/exercises/converter/features/step_definitions/converter_steps.rb +++ b/week7/exercises/converter/features/step_definitions/converter_steps.rb @@ -14,38 +14,3 @@ @result.should eq "#{arg1}.#{arg2}".to_f end -Given /^we have a puppy$/ do - @puppy = Puppy.new() -end - -Given /^its name is (\w)$/ do |arg1| - @puppy.name = arg1 -end - -When /^we pet the puppy$/ do - pending # express the regexp above with the code you wish you had -end - -Then /^the puppy wags its tail$/ do - pending # express the regexp above with the code you wish you had -end - -Given /^its name is Bella$/ do - pending # express the regexp above with the code you wish you had -end - -When /^we ring the bell$/ do - pending # express the regexp above with the code you wish you had -end - -When /^it wags its tail$/ do - pending # express the regexp above with the code you wish you had -end - -Then /^we must take it out$/ do - pending # express the regexp above with the code you wish you had -end - -Then /^then it will not pee on the floor$/ do - pending # express the regexp above with the code you wish you had -end diff --git a/week7/exercises/puppy/features/step_definitions/puppy.rb b/week7/exercises/puppy/features/step_definitions/puppy.rb index e69de29..796537a 100644 --- a/week7/exercises/puppy/features/step_definitions/puppy.rb +++ b/week7/exercises/puppy/features/step_definitions/puppy.rb @@ -0,0 +1,8 @@ +class Puppy + attr_accessor :name + + def initialize(name=nil) + @name = name + end + +end \ No newline at end of file diff --git a/week7/exercises/puppy/features/step_definitions/puppy_steps.rb b/week7/exercises/puppy/features/step_definitions/puppy_steps.rb index e69de29..5be2d2b 100644 --- a/week7/exercises/puppy/features/step_definitions/puppy_steps.rb +++ b/week7/exercises/puppy/features/step_definitions/puppy_steps.rb @@ -0,0 +1,31 @@ +Given /^we have a puppy$/ do + @puppy = Puppy.new() +end + +Given /^its name is (\w+)$/ do |arg1| + @puppy.name = arg1 +end + +When /^we pet the puppy$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^the puppy wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +When /^we ring the bell$/ do + pending # express the regexp above with the code you wish you had +end + +When /^it wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^we must take it out$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^then it will not pee on the floor$/ do + pending # express the regexp above with the code you wish you had +end From 2d2f3c9d9983a0012c7cc160476a323ab6628518 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Mon, 26 Nov 2012 19:59:45 -0800 Subject: [PATCH 15/22] pirate translator --- .../{ => pirate}/features/pirate.feature | 0 .../features/step_definitions/pirate-steps.rb | 19 +++++++++++++++++++ .../features/step_definitions/pirate.rb | 7 +++++++ .../step_definitions/tic-tac-toe-steps.rb | 0 .../features/tic-tac-toe.feature | 0 week7/homework/{ => tictactoe}/play_game.rb | 0 6 files changed, 26 insertions(+) rename week7/homework/{ => pirate}/features/pirate.feature (100%) create mode 100644 week7/homework/pirate/features/step_definitions/pirate-steps.rb create mode 100644 week7/homework/pirate/features/step_definitions/pirate.rb rename week7/homework/{ => tictactoe}/features/step_definitions/tic-tac-toe-steps.rb (100%) rename week7/homework/{ => tictactoe}/features/tic-tac-toe.feature (100%) rename week7/homework/{ => tictactoe}/play_game.rb (100%) diff --git a/week7/homework/features/pirate.feature b/week7/homework/pirate/features/pirate.feature similarity index 100% rename from week7/homework/features/pirate.feature rename to week7/homework/pirate/features/pirate.feature diff --git a/week7/homework/pirate/features/step_definitions/pirate-steps.rb b/week7/homework/pirate/features/step_definitions/pirate-steps.rb new file mode 100644 index 0000000..5d4b939 --- /dev/null +++ b/week7/homework/pirate/features/step_definitions/pirate-steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a PirateTranslator$/ do + @piratetranslator = PirateTranslator.new() +end + +Blimey /^I say 'Hello Friend'$/ do + @piratetranslator.say = "Hello Friend" +end + +Blimey /^I hit translate$/ do + @result = @piratetranslator.translate() +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @result.should =~ /Ahoy Matey/ +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @result.should =~ /Shiber Me Timbers You Scurvey Dogs!!/ +end diff --git a/week7/homework/pirate/features/step_definitions/pirate.rb b/week7/homework/pirate/features/step_definitions/pirate.rb new file mode 100644 index 0000000..8b6839d --- /dev/null +++ b/week7/homework/pirate/features/step_definitions/pirate.rb @@ -0,0 +1,7 @@ +class PirateTranslator + attr_accessor :say + + def translate + "Ahoy Matey Shiber Me Timbers You Scurvey Dogs!!" + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb similarity index 100% rename from week7/homework/features/step_definitions/tic-tac-toe-steps.rb rename to week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/tictactoe/features/tic-tac-toe.feature similarity index 100% rename from week7/homework/features/tic-tac-toe.feature rename to week7/homework/tictactoe/features/tic-tac-toe.feature diff --git a/week7/homework/play_game.rb b/week7/homework/tictactoe/play_game.rb similarity index 100% rename from week7/homework/play_game.rb rename to week7/homework/tictactoe/play_game.rb From 327c4d329bcb0fd8262b023fdcd0c6f82da48bec Mon Sep 17 00:00:00 2001 From: scurtiss Date: Tue, 27 Nov 2012 16:08:49 -0800 Subject: [PATCH 16/22] Homework Question Answers --- week7/homework/questions.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..62641c8 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -1,9 +1,18 @@ - Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +method_missing is an hook triggered when an object is sent a message that it can handle. It can be used to dynamically create and handle method calls that aren’t implicitly defined. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An Eigenclass is another name for a singleton class. Singleton methods live with the anonymous class it’s associated with. + 3. When would you use DuckTypeing? How would you use it to improve your code? +You use ducktyping when you think of objects based on what they can do instead of their explicit type. You can use it to improve your code by not checking the object types all of the time and make more generalized methods that can handle any object that behave a certain way. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class. +Class_eval sets things up as if you were in the body of a class definition whereas instance_eval sets things up as if you were inside the singleton class of self. + 5. What is the difference between a singleton class and a singleton method? +A singleton class is a class which defines a single object whereas a singleton method is a method that belongs to a single object. \ No newline at end of file From 9bcaa8a58b7242fbc12ba2230cfd2ae576261fda Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sun, 2 Dec 2012 15:31:17 -0800 Subject: [PATCH 17/22] ?? --- week8/exercises/couch.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..3314697 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -4,17 +4,17 @@ def initialize(pillows, cushions) @cushions = cushions end - def pillow_colors - @pillows.join(", ") - end - - def cushion_colors - @cushions.join(", ") - end - [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count - end - end + end + define_method("#{s}".chomp() +"_colors") do + instance_variable_get("@#{s}").join(", ") + end + end + + def method_missing(meth, *args, &block) + puts "You called #{meth} with #{args.join(", ")}" + super + end end From 660cccb4d97287af67b016d17c27306185448a2a Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sun, 2 Dec 2012 16:10:08 -0800 Subject: [PATCH 18/22] class and methods defined --- .../step_definitions}/play_game.rb | 0 .../features/step_definitions/tic-tac-toe.rb | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+) rename week7/homework/tictactoe/{ => features/step_definitions}/play_game.rb (100%) create mode 100644 week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb diff --git a/week7/homework/tictactoe/play_game.rb b/week7/homework/tictactoe/features/step_definitions/play_game.rb similarity index 100% rename from week7/homework/tictactoe/play_game.rb rename to week7/homework/tictactoe/features/step_definitions/play_game.rb diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..ffaa40c --- /dev/null +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,23 @@ +class TicTacToe + attr_accessor :player, :gameActive, :playerWon, :computerWon + + def welcome_player + :player + end + + def over? + :gameActive + end + + def player_won? + :playerWon + end + + def computer_won? + :computerWon + end + + def draw? + :playerWon == false and :computerWon == false + end +end \ No newline at end of file From 84cab62cbe255a2b1e0199ec4ce5fe3f46b601de Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Tue, 4 Dec 2012 02:56:26 -0800 Subject: [PATCH 19/22] Redo start methods --- .../features/step_definitions/tic-tac-toe.rb | 63 +++++++++++++++++-- .../step_definitions => }/play_game.rb | 0 2 files changed, 57 insertions(+), 6 deletions(-) rename week7/homework/tictactoe/{features/step_definitions => }/play_game.rb (100%) diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb index ffaa40c..f251529 100644 --- a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -1,23 +1,74 @@ class TicTacToe - attr_accessor :player, :gameActive, :playerWon, :computerWon + attr_accessor :player, :player_symbol, :winner + attr_reader :over, :currentplayer, :computer_symbol + + SYMBOLS = [:O, :X] + + def initialize(currentplayer = nil, symbol = nil) + @over = false + @winner = :none + + if currentplayer != nil then + @currentplayer = currentplayer + else + @currentplayer = (Random.new.rand(0..1) == 0 ? :player : :computer) + end + + if symbol != nil then + @player_symbol = symbol + @computer_symbol = (symbol == :O ? :X : :O) + else + @player_symbol = SYMBOLS[0] + @computer_symbol = SYMBOLS[1] + end + end def welcome_player - :player + "Welcome #{@player}" + end + + def indicate_palyer_turn + "It is Your Turn to Move" + end + + def current_player + @currentplayer == :computer ? "Computer" : @player + end + + def current_state + #TODO output board and game status + end + + def player_move + get_player_move + end + + def get_player_move + @player_move = gets() + end + + def computer_move + #TODO have computer select random open spot + end + + def determine_winner + #TODO check to see if game is over and set appropriate variables + @over = true end def over? - :gameActive + @over end def player_won? - :playerWon + :winner == "#{player}" end def computer_won? - :computerWon + :winner == "Computer" end def draw? - :playerWon == false and :computerWon == false + :winner == "Draw" end end \ No newline at end of file diff --git a/week7/homework/tictactoe/features/step_definitions/play_game.rb b/week7/homework/tictactoe/play_game.rb similarity index 100% rename from week7/homework/tictactoe/features/step_definitions/play_game.rb rename to week7/homework/tictactoe/play_game.rb From 27987b329db56095b6db39cdb373ddbe39a56bc1 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Tue, 4 Dec 2012 20:49:20 -0800 Subject: [PATCH 20/22] Added board and methods --- .../features/step_definitions/tic-tac-toe.rb | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb index f251529..49a2520 100644 --- a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -1,5 +1,5 @@ class TicTacToe - attr_accessor :player, :player_symbol, :winner + attr_accessor :player, :player_symbol, :winner, :board attr_reader :over, :currentplayer, :computer_symbol SYMBOLS = [:O, :X] @@ -21,6 +21,12 @@ def initialize(currentplayer = nil, symbol = nil) @player_symbol = SYMBOLS[0] @computer_symbol = SYMBOLS[1] end + + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } end def welcome_player @@ -36,11 +42,13 @@ def current_player end def current_state - #TODO output board and game status + @board.values end def player_move - get_player_move + set_position(get_player_move.to_sym, @player_symbol) + @currentplayer = :computer + get_player_move.to_sym end def get_player_move @@ -48,10 +56,28 @@ def get_player_move end def computer_move - #TODO have computer select random open spot + @pick = open_spots.sample() + set_position(@pick, @computer_symbol) + @currentplayer = :player + @pick + end + + def open_spots + @board.select{|k,v| v == " "}.keys + end + + def spots_open? + open_spots.count() > 0 + end + + def set_position(position, symbol) + #TODO check for a valid move + @board[position] = symbol.to_s end def determine_winner + @winning_symbol = nil + #@board.select{|k,v| v!=" "}.keys.join.count("A") #TODO check to see if game is over and set appropriate variables @over = true end From bf42193524cdd2a0f5ca55436909ac502d089e90 Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Thu, 6 Dec 2012 23:10:42 -0800 Subject: [PATCH 21/22] All cucumber scenarios pass --- .../features/step_definitions/tic-tac-toe.rb | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb index 49a2520..0ee5cef 100644 --- a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -76,10 +76,45 @@ def set_position(position, symbol) end def determine_winner - @winning_symbol = nil - #@board.select{|k,v| v!=" "}.keys.join.count("A") - #TODO check to see if game is over and set appropriate variables - @over = true + ["A","B","C","1","2","3"].each do |v| + if check_row(@player_symbol, v) then + @winner = :player + end + end + if check_diagonal(@player_symbol) then + @winner = :player + end + + + #TODO Check diagnol for player + ["A","B","C","1","2","3"].each do |v| + if check_row(@computer_symbol, v) then + @winner = :computer + @over = true + end + end + if check_diagonal(@computer_symbol) then + @winner = :computer + end + if @winner != :none then + @over = true + end + + #TODO Consolidate copied code + if not over? and not spots_open? then + puts("Draw") + @winner = :draw + @over = true + end + end + + def check_row(symbol, row) + @board.select{|k,v| v==symbol}.keys.join.count(row) == 3 + end + + def check_diagonal(symbol) + (@board[:A1].to_s + @board[:B2].to_s + @board[:C3].to_s).count(symbol.to_s) == 3 || + (@board[:A3].to_s + @board[:B2].to_s + @board[:C1].to_s).count(symbol.to_s) == 3 end def over? @@ -87,14 +122,14 @@ def over? end def player_won? - :winner == "#{player}" + @winner == :player end def computer_won? - :winner == "Computer" + @winner == :computer end def draw? - :winner == "Draw" + @winner == :draw end end \ No newline at end of file From 498b75510c699de8a9dcc3d6b8098d68d9e33f2e Mon Sep 17 00:00:00 2001 From: Steven Curtiss Date: Sat, 8 Dec 2012 15:08:29 -0800 Subject: [PATCH 22/22] Made the game work with the play_game file --- .../features/step_definitions/tic-tac-toe.rb | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb index 0ee5cef..8c506b4 100644 --- a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -7,6 +7,7 @@ class TicTacToe def initialize(currentplayer = nil, symbol = nil) @over = false @winner = :none + @player = "Player" if currentplayer != nil then @currentplayer = currentplayer @@ -30,11 +31,12 @@ def initialize(currentplayer = nil, symbol = nil) end def welcome_player + puts("Welcome #{@player}") "Welcome #{@player}" end def indicate_palyer_turn - "It is Your Turn to Move" + puts("#{@player}'s Move:") end def current_player @@ -42,6 +44,7 @@ def current_player end def current_state + puts("Board = #{@board}") @board.values end @@ -52,11 +55,12 @@ def player_move end def get_player_move - @player_move = gets() + @player_move = gets().chomp end def computer_move @pick = open_spots.sample() + puts("Computer chose: #{@pick}") set_position(@pick, @computer_symbol) @currentplayer = :player @pick @@ -71,8 +75,13 @@ def spots_open? end def set_position(position, symbol) - #TODO check for a valid move - @board[position] = symbol.to_s + if(@board[position] != " ") then + @random_move = open_spots.sample() + puts("Spot taken, choosing random spot: #{@random_move}") + @board[@random_move] = symbol.to_s + else + @board[position] = symbol.to_s + end end def determine_winner @@ -85,8 +94,6 @@ def determine_winner @winner = :player end - - #TODO Check diagnol for player ["A","B","C","1","2","3"].each do |v| if check_row(@computer_symbol, v) then @winner = :computer @@ -100,7 +107,6 @@ def determine_winner @over = true end - #TODO Consolidate copied code if not over? and not spots_open? then puts("Draw") @winner = :draw @@ -109,7 +115,7 @@ def determine_winner end def check_row(symbol, row) - @board.select{|k,v| v==symbol}.keys.join.count(row) == 3 + @board.select{|k,v| v==symbol.to_s}.keys.join.count(row) == 3 end def check_diagonal(symbol)