From 31a0c3788cb343d2ca4edc129bbf6c96bb534d96 Mon Sep 17 00:00:00 2001 From: mattsface Date: Fri, 10 Jan 2014 21:08:19 -0800 Subject: [PATCH 01/15] updated questions and rspec_spec.rb --- week1/exercises/rspec_spec.rb | 13 +++++++++---- week1/homework/questions.txt | 10 +++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index b00c711..2d188f9 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -77,15 +77,20 @@ # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - (1+2-5*6/2).should eq -6 + (((1+2)-5)*(6/2)).should eq -6 end + it "should count the characters in your name" do - pending + "Matt".should have(4).characters end - it "should check basic math" + it "should check basic math" do + (4+4-2).should eq 6 + end - it "should check basic spelling" + it "should check basic spelling" do + "Matt".should include("Matt") + end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..e336cb0 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,20 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is an instance of a class. + 2. What is a variable? +Variables holds a reference to an object or data + 3. What is the difference between an object and a class? +A class is the code or container that has properties ( methods / variable ) that can be used to create an instance of an object. A object is an instance of that class. That class can be used to create many objects. + 4. What is a String? +A string are a sequence of characters, for example "Testing", "This is a string.", and even "324234". + 5. What are three messages that I can send to a string object? Hint: think methods -6. What are two ways of defining a String literal? Bonus: What is the difference between them? +6. What are two ways of defining a String literal? Bonus: What is the difference between them? \ No newline at end of file From ccedeef34d0ee0291000f92f51947e6721a24a3f Mon Sep 17 00:00:00 2001 From: mattsface Date: Sat, 11 Jan 2014 11:00:44 -0800 Subject: [PATCH 02/15] Finished homework --- week1/homework/questions.txt | 31 +++++++++++++++++++++--- week1/homework/strings_and_rspec_spec.rb | 17 +++++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index e336cb0..f36c970 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,15 +4,15 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? -An object is an instance of a class. +An object is an instance of a class that is represented in memory. 2. What is a variable? -Variables holds a reference to an object or data +Variables holds a reference to an object or data. There are different types of variables with different scopes. 3. What is the difference between an object and a class? -A class is the code or container that has properties ( methods / variable ) that can be used to create an instance of an object. A object is an instance of that class. That class can be used to create many objects. +A class is the code or container that has properties ( methods / variable ) that can be used to create an instance of an object. A object is an instance of that class. 4. What is a String? @@ -20,4 +20,27 @@ A string are a sequence of characters, for example "Testing", "This is a string. 5. What are three messages that I can send to a string object? Hint: think methods -6. What are two ways of defining a String literal? Bonus: What is the difference between them? \ No newline at end of file +str.capitalize + This will capitalize a string + +str.downcase + returns a copy of the string where every uppercase character in the string is a lowercase + +str.hash + returns a hash of the string + +6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +Good lord, I've never seen so many ways to define a string literal in a language before. What is the best practice here? + +I'll the ones I find most interesting: + +%q and %Q can be used to create string literals, and the difference between them is the degree of substitution performed. + +%q is the equivalent to a single-quoted delimited string, and a substitution is performed by using a single "\". + +%q!This is a test string string! + +%Q is the equivalent to a double-quote delimited string, and quoting the book supports a "boatload" more escape sequences. They reference the most common one, the new line sequence "\n" + +%Q!Yada yada double quoted string! \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..3ffb40c 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,25 +1,20 @@ # encoding: utf-8 -# Please make these examples all pass -# You will need to change the 3 pending tests -# You will need to write a passing test for the first example -# (Hint: If you need help refer to the in-class exercises) -# The two tests with the pending keyword, require some ruby code to be written -# (Hint: You should do the reading on Strings first) - describe String do context "When a string is defined" do 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 count the charaters" do + @my_string.should have(66).charaters + end + it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @my_string.split(".") result.should have(2).items end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + (@my_string.encoding).should eq (Encoding.find("UTF-8")) end end end From 70ca1ea3771d4afe46ec3b8b68ebf53c867ce523 Mon Sep 17 00:00:00 2001 From: mattsface Date: Thu, 16 Jan 2014 22:08:31 -0800 Subject: [PATCH 03/15] added simon_says.rb --- week2/homework/simon_says.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..d0abac3 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,27 @@ + + +class SimonSays + def initialize(word) + @word = word + end + + def echo + puts "#{SimonSays.word}" + end + + def shout + puts "#{SimonSays.word.capitalize}" + end + + def repeat + + end + + def start_of_word + + end + + def first_word + + end +end From 1463da149a4d4e881bdf4af64c81677e4a857b94 Mon Sep 17 00:00:00 2001 From: mattsface Date: Sat, 18 Jan 2014 11:59:27 -0800 Subject: [PATCH 04/15] Added homework and updated questions.txt --- week2/homework/questions.txt | 12 ++++++++++++ week2/homework/simon_says.rb | 32 ++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..a401d54 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,22 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + An array is an ordered list of elements that are accessed using an index number. A Hash is a container of unordered elements that are stored in a map => key fashion. The values are accessed by calling the key, e.g Hash['map']. + 2. When would you use an Array over a Hash and vice versa? + Arrays are used when speed is important and elements need to be organized in an ordered fashion. Hashes are used when data needs to be accessed by a key, not the index. + 3. What is a module? Enumerable is a built in Ruby module, what is it? + A module is a library of reusable code that can be imported into other code. Modules can contain methods, classes, and constants. Emumerable is module that allows your classes to support many of Rubys popular methods like ".include?" + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + Yes you can, well, I'm just throwing this out there, I'm probably wrong :), but from my understanding inheritance looks like it works in a hierarchical fashion. So if you want to inherit from more than one class, you'll have to structure your classes in a manner that allow the inheritance to flow up the chain. I'm having trouble articulating this, so I'll try and draw it. + + Parent Class <--- Teir 1 class <---- Teir 2 class <----- Teir 3 classs + 5. What is the difference between a Module and a Class? + + A Module can't be used to create an instance. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index d0abac3..b615e0e 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,27 +1,27 @@ +module SimonSays - -class SimonSays - def initialize(word) - @word = word - end - - def echo - puts "#{SimonSays.word}" + # return the string + def echo(string) + return "#{string}" end - def shout - puts "#{SimonSays.word.capitalize}" + # return string in all capitals + def shout(string) + return "#{string.upcase}" end - def repeat - + # repeat the string a min of 2 times, unless specified + def repeat(string, repeat=2) + return ("#{string} " * repeat).strip end - def start_of_word - + # return the charaters starting from start of string to length + def start_of_word(string, length) + return "#{string[0, length]}" end - def first_word - + # return first word in a string using a space has a delimiter + def first_word(string) + return "#{string.split(" ")[0]}" end end From 7401d4d355fc9466c9b34cb401a2abbf3d424f37 Mon Sep 17 00:00:00 2001 From: mattsface Date: Sat, 18 Jan 2014 17:53:20 -0800 Subject: [PATCH 05/15] updated simon_says.rb, removed the returns --- week2/homework/simon_says.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index b615e0e..f5c39c9 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -2,26 +2,26 @@ module SimonSays # return the string def echo(string) - return "#{string}" + "#{string}" end # return string in all capitals def shout(string) - return "#{string.upcase}" + "#{string.upcase}" end # repeat the string a min of 2 times, unless specified def repeat(string, repeat=2) - return ("#{string} " * repeat).strip + ("#{string} " * repeat).strip end # return the charaters starting from start of string to length def start_of_word(string, length) - return "#{string[0, length]}" + "#{string[0, length]}" end # return first word in a string using a space has a delimiter def first_word(string) - return "#{string.split(" ")[0]}" + "#{string.split(" ")[0]}" end end From 6ba2ff13d993a6d05534c8900cd6b52b7e8d5089 Mon Sep 17 00:00:00 2001 From: mattsface Date: Thu, 30 Jan 2014 08:19:06 -0800 Subject: [PATCH 06/15] homework/calculator.rb --- week3/homework/questions.txt | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..b818c18 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,30 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + They are similar to a string, and can be used to represent strings or names, but unlike strings they are created only once in memory and are immutable. 2. What is the difference between a symbol and a string? - + One simple difference is that symbols are immutable and strings are mutable. Symbols are only created once in memory, unlike strings. + 3. What is a block and how do I call a block? + A block is similar to a nameless function. They are always called in conjunction with a method, and wrapped in braces or a do/end. You can call a block by calling a method on a certain object, and then following up with brackets or the do/end. For example + + array = [1, 2, 3, 4, 5] + array.each { |n| puts n } + + Blocks are probably my favorite part of Ruby so far :) 4. How do I pass a block to a method? What is the method signature? + You can pass a block to a method by using the yield command. + + def testmethod + yield + end + + testmethod { puts "This text" } => "This text" + + The method signature is the method name, the arguments, and the order of the arugments. + 5. Where would you use regular expressions? + There are lots of places to use a regular expression, parsing log files, trying to match certain fields in text files, and finding hidden treasure. \ No newline at end of file From 024c9d8821dc2f954f3680306bb708f8e4de0fec Mon Sep 17 00:00:00 2001 From: mattsface Date: Thu, 30 Jan 2014 08:20:45 -0800 Subject: [PATCH 07/15] added calculator.rb --- week2/exercises/book.rb | 28 ++++++--------- week2/exercises/book_spec.rb | 58 +++++++++++-------------------- week3/homework/calculator.rb | 35 +++++++++++++++++++ week3/homework/calculator_spec.rb | 2 +- 4 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 week3/homework/calculator.rb diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index a6b943d..f81a3c3 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,27 +1,21 @@ class Book - attr_accessor :title - attr_reader :page_count - @@book_count = 0 + attr_accessor :pages, :title - def self.book_count - @@book_count - end + @@library_count = 0 - def initialize title = "Not Set", page_count = 0 - @@book_count += 1 - @page_count = page_count + def initialize pages = 0, title="N/A" + @pages = pages @title = title + @@library_count += 1 end - - def test - @test = "Hello!" + def self.library_count + @@library_count end - - def out_put_test - puts @test - puts @@book_count + + def happy + "There are #{@pages} happy pages in this book" end -end \ No newline at end of file +end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..ade4753 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,49 +1,33 @@ -require './book' +require './book.rb' -describe Book do +describe Book do - - context "::book_count" do + before :each do + @book = Book.new 542, "Programming Ruby" + end - it "should count how many books have been created" do - Book.new - Book.new - Book.new - Book.book_count.should eq 3 + context "::library_count" do + it "should tell us how many books we have" do + 34233.times{ Book.new 3 } + Book.library_count.should eq 34234 end - end - context "::new" do - - it "should set some defaults" do - Book.new.title.should eq "Not Set" + context "#pages" do + it "should have a pages" do + @book.should respond_to "pages" end - it "should allow us to set the page count" do - book = Book.new "Harry Potter", 5 - book.page_count.should eq 5 + it "should allow us to set the number" do + @book.pages = 542 + @book.pages.should eq 542 + end end - end - - context "#title" do - - before :each do - @book = Book.new + context "#title" do + it "should let us set and get a title" do + @book.title = "Programming Ruby" + @book.title.should eq "Programming Ruby" + end end - - it "should have a title" do - @book.should respond_to "title" - end - - it "should allow me to set the title" do - @book.title = "Snow Crash" - @book.title.should eq "Snow Crash" - end - - - - end - end \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..b4acc6e --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,35 @@ +class Calculator + + # method to check if args passed to method is a multidimensional array + def is_dim(var) + var.each do |x| + true if x.is_a?(Array) + end + false + end + + def sum(*args) + args.flatten! if is_dim(args) # flatten array if its multidimensional + args.inject(0) {|sum, element| sum+element} + end + + def multiply(*args) + args.flatten! if is_dim(args) # flatten array if its multidimensional + args.inject(1) {|product, element| product*element} + end + + # derp + def pow(x, y) + x ** y + end + + def fac(num) + # convert num to absolute value, then inject to find the factorial unless num == 0, then return 1 + ( (1..num.abs).inject { |factorial, n| factorial * n } unless num == 0 ) || ( 1 ) + end + + +end + + + diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..c7a2393 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,6 +1,6 @@ require "#{File.dirname(__FILE__)}/calculator" -describe Calculator do +describe Calculator do before do @calculator = Calculator.new From e68f791bdc904f72092238bab406faf8fb66b073 Mon Sep 17 00:00:00 2001 From: Matthew Spah Date: Thu, 30 Jan 2014 08:29:46 -0800 Subject: [PATCH 08/15] Update rspec_spec.rb --- week1/exercises/rspec_spec.rb | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index ca93376..3a6295f 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -73,7 +73,6 @@ end context "Examples for in-class test exploration" do -<<<<<<< HEAD it "should know order of operations" do # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: @@ -91,24 +90,6 @@ it "should check basic spelling" do "Matt".should include("Matt") -======= - it "should know order of operations" do - # Fix the Failing Test - # Order of Operations is Please Excuse My Dear Aunt Sally: - # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - ((((1+2)-5)*6)/2).should eq -6 - end - it "should count the characters in your name" do - "Tom".should have(3).characters - end - - it "should check basic math" do - (40+2).should eq 42 - end - - it "should check basic spelling" do - "Field".should include('ie') ->>>>>>> 4981dd20c06248919b350fa8600269a41e169e64 end end From b254a0e918fa8aa0369a6d300b6eec55c1127fd3 Mon Sep 17 00:00:00 2001 From: mattsface Date: Thu, 30 Jan 2014 09:17:21 -0800 Subject: [PATCH 09/15] updated calculator --- week3/homework/calculator.rb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index b4acc6e..6cc67fe 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,23 +1,14 @@ class Calculator - - # method to check if args passed to method is a multidimensional array - def is_dim(var) - var.each do |x| - true if x.is_a?(Array) - end - false - end - + def sum(*args) - args.flatten! if is_dim(args) # flatten array if its multidimensional + args.flatten! args.inject(0) {|sum, element| sum+element} end def multiply(*args) - args.flatten! if is_dim(args) # flatten array if its multidimensional + args.flatten! args.inject(1) {|product, element| product*element} end - # derp def pow(x, y) x ** y From 83561ceedf0eaa10664f465d596be77cbed46120 Mon Sep 17 00:00:00 2001 From: mattsface Date: Tue, 4 Feb 2014 10:14:42 -0800 Subject: [PATCH 10/15] updated worker.rb --- week4/homework/worker.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..e77b043 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,9 @@ +class Worker + + def Worker.work(n=1) + x = nil + n.times { x = yield } + x + end +end + From 41ddf17e7607179ab3c9fc14699693d8b8e3c7d7 Mon Sep 17 00:00:00 2001 From: mattsface Date: Thu, 6 Feb 2014 15:47:49 -0800 Subject: [PATCH 11/15] added questions and worker.rb --- week4/homework/questions.txt | 31 +++++++++++++++++++++++++++++++ week4/homework/worker.rb | 2 ++ 2 files changed, 33 insertions(+) diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..857030e 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,11 +4,42 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +It reads by opening up a opening and closing IO streams, which allows you to read and write data to files, standard input, and standard output. + +One way to open a file is to use the File class, which is a subclass of the IO class. + +file = File.new("Matts.txt", "r") # this would open the file Matts.txt for reading only + 2. How would you output "Hello World!" to a file called my_output.txt? +First, you need to open the file for writing. + +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 Object Dir class is a class that allows you to manulape directories, list directories, and poke in side directories. It looks like its a great class for some shell shellish scripting :) + 4. What is an IO object? +The IO object is an object that allows you to open and close streams of bytes that allow to you read and write data. It has many subclasses that include File and Directory. + 5. What is rake and what is it used for? What is a rake task? +Rake is a build tool build and compile large software tools. It's very similar to make. + +A rake task is a rake task... For example, Here's an example I found on the internet: + +task :manipulate_files do + mkdir 'new_dir' + mv 'new_dir', 'lukasz' + chmod 0777, 'lukasz' + touch 'lukasz/wrobel.txt' +end + +We could use something like this to set up an install envirement for our ruby program. + + + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb index e77b043..9c60fac 100644 --- a/week4/homework/worker.rb +++ b/week4/homework/worker.rb @@ -1,5 +1,7 @@ class Worker + # set x to nil, and then append yield to x for n times + # then return x def Worker.work(n=1) x = nil n.times { x = yield } From 57bf71327ca1c03d24114259e2031ca1fb5e3e42 Mon Sep 17 00:00:00 2001 From: mattsface Date: Sun, 23 Feb 2014 16:33:49 -0800 Subject: [PATCH 12/15] Umm.. is this how the home should look? lol --- week7/exercises/features/converter.feature | 8 ++++---- .../features/step_definitions/piratetranslator.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 week7/homework/features/step_definitions/piratetranslator.rb diff --git a/week7/exercises/features/converter.feature b/week7/exercises/features/converter.feature index 5e262a8..f75df32 100644 --- a/week7/exercises/features/converter.feature +++ b/week7/exercises/features/converter.feature @@ -4,14 +4,14 @@ Feature: Converting metric I want to convert a Fahrenheit temperature to Celsius Scenario: - Given I have entered 32 into the converter - And I set the type to Fahrenheit + Given I have entered "32" into the converter + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 0.0 Scenario: - Given I have entered 75 into the converter - And I set the type to Fahrenheit + Given I have entered "75" into the converter + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 23.9 diff --git a/week7/homework/features/step_definitions/piratetranslator.rb b/week7/homework/features/step_definitions/piratetranslator.rb new file mode 100644 index 0000000..f1325f6 --- /dev/null +++ b/week7/homework/features/step_definitions/piratetranslator.rb @@ -0,0 +1,12 @@ +class PirateTranslator + + def say(english) + @english = english + end + + def translate + "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end + +end + From c7f9cda08e78f7b47c57cb2c22842345a9e84941 Mon Sep 17 00:00:00 2001 From: mattsface Date: Tue, 25 Feb 2014 21:32:43 -0800 Subject: [PATCH 13/15] homework --- .../step_definitions/piratetranslator.rb | 5 ++++- week7/homework/questions.txt | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/step_definitions/piratetranslator.rb b/week7/homework/features/step_definitions/piratetranslator.rb index f1325f6..8b8fe01 100644 --- a/week7/homework/features/step_definitions/piratetranslator.rb +++ b/week7/homework/features/step_definitions/piratetranslator.rb @@ -1,12 +1,15 @@ class PirateTranslator + # derp def say(english) @english = english end - + + # herp def translate "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" end end + \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..8d85b49 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,23 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +method_missing is a method that can be built into an object that gets called when a "unknown method" is called on the object. So instead of the program exiting, you can handle the error on your own. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +The Eigenclass is the "ghost class" that is created when a Singleton method is applied to a class. The objects orignal class is now a superclass, while the Eigenclass is the objects current class. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +You use DuckTyping when you want to code around what an object can do, not by its class. This will allow you remove lots of "type" checking you see in other languages, and less error handling! I think anyway! + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +A class method can be called without actually creating an instance of that class, for example the File Class has a lot of methods that can be used without creating an instance of that class. Instance methods belong to an instance of a class. + +instance_eval allows you to eval instance variables within a class while class_eval allows you to add methods within classes.. That's what I gather from my reading + 5. What is the difference between a singleton class and a singleton method? + +Is there a difference? It looks like they are both used to define certain methods to a certain object rather than writing those methods into its defiing class. From 4739187381d206f7250e709c567d36fcf4db0160 Mon Sep 17 00:00:00 2001 From: "spahmatthew@gmail.com" Date: Wed, 12 Mar 2014 21:33:54 -0700 Subject: [PATCH 14/15] all done --- week7/homework/features/step_definitions/tic-tac-toe-steps.rb | 2 +- week7/homework/play_game.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..a9bd88e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..7b99f10 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -10,7 +10,7 @@ when "Computer" @game.computer_move when @game.player - @game.indicate_palyer_turn + @game.indicate_player_turn @game.player_move end puts @game.current_state From 9122c5fdc7658edd3c1719cbbe330271f3262a1a Mon Sep 17 00:00:00 2001 From: "spahmatthew@gmail.com" Date: Wed, 12 Mar 2014 21:41:31 -0700 Subject: [PATCH 15/15] final all done --- .../features/step_definitions/tic-tac-toe.rb | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..916b4e1 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,169 @@ +class TicTacToe + attr_accessor :board, :player, :player_symbol, :computer_symbol + SYMBOLS = [:O, :X] + + def initialize(current_player=nil, user_symbol=nil) + + unless current_player + @current_player = [:player, :computer].sample + else + @current_player = current_player + end + + unless user_symbol + random_symbol + else + @user_symbol = user_symbol + if @user_symbol == :X + @player_symbol = @user_symbol + @computer_symbol = :O + else + @player_symbol = @user_symbol + @computer_symbol = :X + end + end + + @board = { A1: ' ', A2: ' ', A3: ' ', + B1: ' ', B2: ' ', B3: ' ', + C1: ' ', C2: ' ', C3: ' ' } + + @winner = nil + + @win_rows = {one: [:A1, :A2, :A3], two: [:B1, :B2, :B3], three: [:C1, :C2, :C3], + four: [:A1, :B1, :C1], five: [:A2, :B2, :C2], six: [:A3, :B3, :C3], + seven: [:A1, :B2, :C3], eight: [:A3, :B2, :C1] + } + + end + + def determine_winner + @xarray = [] + @oarray = [] + + @board.each do |key, value| + @xarray << key if value == :X + @oarray << key if value == :O + end + + @win_rows.each do |win, spots| + if spots == spots & @xarray + if :X == @player_symbol + @winner = @player + else + @winner = "Computer" + end + elsif spots == spots & @oarray + if :O == @player_symbol + @winner = @player + else + @winner = "Computer" + end + else + draw? + end + end + end + + + + def open_spots + @open = [] + @board.each { |key, value| @open << key if value == ' '} + @open + end + + def computer_move + indicate_player_turn + @computer_move = @open.sample + @board[@computer_move] = @computer_symbol + @current_player = :player + @computer_move + end + + def random_start + [:player, :computer].sample + end + + def current_state + @current_game = "" + @game = @board.values.map { |value| value } + @game.each_slice(3) { |x| @current_game << "#{x.join("|")}\n_____\n" } + @current_game + end + + def indicate_player_turn + puts "#{current_player}'s Move:" + end + + def get_player_move + @pmove = gets.chomp + @pmove = @pmove.capitalize + @pmove + end + + def player_move + @move = get_player_move.to_sym + if open_spots.include?(@move) + @board[@move] = @player_symbol + else + spot_taken + player_move + end + @current_player = :computer + @move + end + + + def random_symbol + @player_symbol = SYMBOLS.sample + if @player_symbol == :X + @computer_symbol = :O + else + @computer_symbol = :X + end + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + @players = {player: @player, computer: "Computer"} + @players[@current_player] + end + + def spot_taken + puts "Try again, that spot is taken #{current_player}" + end + + def player_won? + if @winner == @player + true + end + end + + def computer_won? + if @winner == "Computer" + true + end + + end + + def spots_open? + !open_spots.empty? + end + + def draw? + if !computer_won? && !player_won? && !spots_open? + true + end + end + + def over? + if draw? || computer_won? || player_won? + true + end + end + + +end