From e2e92ae88aab0dad6bcbb9358704a5f4e1fb3edb Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 16 Oct 2012 15:03:17 -0700 Subject: [PATCH 01/27] Completed Week1 exercises --- week1/exercises/rspec_spec.rb | 37 ++++++++++++++---------- week1/homework/questions.txt | 36 +++++++++++++++++++---- week1/homework/strings_and_rspec_spec.rb | 30 +++++++++---------- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 31800c8..e7e8291 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -1,7 +1,7 @@ # encoding: utf-8 describe "The Rspec ruby gem" do - + context "Domain Specific Language" do it "creates examples with the #it keyword" do @@ -43,11 +43,13 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 2.should eq 2 end - it "supports placeholder examples that lack code (like this one)" + it "supports placeholder examples that lack code (like this one)" do + %w(Cheese Chester Sandwich).count.should eq 3 + end it "requires that examples use expectations (like #should) to work properly" do @@ -63,25 +65,30 @@ end it "should count the characters in my name" do - "Renée".should have(5).characters + "Renée".should have(5).characters end it "should check how to spell my name" do - "Renée".should include("ée") + "Renée".should include("ée") end end context "Examples for in-class test exploration" do - 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 -10 - end - it "should count the charaters in your name" - it "should check basic math" - it "should check basic spelling" + 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+4-5*6/2).should eq -10 + end + it "should count the charaters in your name" do + "Nikky".should have(5).characters + end + it "should check basic math" do + (20*20).should eq 400 + end + it "should check basic spelling" do + "discombobulation".should eq "discombobulation" + end end - end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 1f41026..7aeb74d 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,6 +1,30 @@ -1. What is an object? -2. What is a variable? -3. What is the difference between an object and a class? -4. What 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 the two? +Q1. What is an object? + +A1. An object is a particular instance of a defined class. + + +Q2. What is a variable? + +A2. A variable is a reference to a specific object; the variable itself is not a object. + + +Q3. What is the difference between an object and a class? + +A3. A class defines an object; an object is a specific instance of a class. + + +Q4. What is a String? + +A4. A string is an object class that contains a specific sequence of characters. + + +Q5. What are three messages that I can send to a string object? Hint: think methods + +A5. .split, .chomp, .squeeze + + +Q6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + +A6. Single Quotes: when defined using single quotes, the string content is not parsed or substituted in any way, other than that single quotes that are part of the string must be prefixed with a backslash. + +Double quotes: when defined using double quotes, many different types of substitutions are performed. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ec1662f..24b9bf6 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,18 +1,18 @@ # encoding: utf-8 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 split on the . charater" do - pending - result = #do something with @my_string here - 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")) - end - end + 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" do + @my_string.should have(66).characters + end + it "should be able to split on the . charater" do + result = @my_string.split('.') + result.should have(2).items + end + it "should be able to give the encoding of the string" do + @my_string.encoding.should eq (Encoding.find("UTF-8")) + end + end end From fb4865b1ae396e6037e1dd8b9fa3721fda929ae7 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Wed, 17 Oct 2012 10:08:42 -0700 Subject: [PATCH 02/27] In-class exercises --- week2/exercises/book.rb | 10 ++++++++++ week2/exercises/book_spec.rb | 5 ++++- week2/exercises/mad_libs.rb | 9 +++++---- 3 files changed, 19 insertions(+), 5 deletions(-) mode change 100644 => 100755 week2/exercises/mad_libs.rb diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index e05e468..176565b 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,2 +1,12 @@ class Book + attr_reader :title, :author, :isbn + def initialize(title, page_count, author="N/A", isbn="N/A") + @title = title + @author = author + @isbn = isbn + @page_count = page_count + end + def page_count + "Page count is #{@page_count}" + end end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index f01bb91..b7538b3 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -4,10 +4,13 @@ @book = Book.new("Harry Potter", 200) end it "should respond to title" do - @book.should respond_to "title" + @book.should respond_to :title end it "should return the page count" do @book.page_count.should eq "Page count is 200" end + it "should respond to isbn" do + @book.should respond_to :isbn + end end diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb old mode 100644 new mode 100755 index 128ee92..e63fc7c --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -1,8 +1,9 @@ -puts "Please enter a noun" +#!/usr/bin/env ruby +puts "Please enter a ballin' noun" noun = gets.chomp -puts "Please enter an adjective" +puts "Please enter a slammin' adjective" adjective = gets.chomp -puts "Please enter a past tense action verb" +puts "Please enter a past tense action-y verb" verb_past_tense = gets.chomp -story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard" +story = "The #{adjective} #{noun} #{verb_past_tense} around champville, home of the awesome squad." puts story From da24083b9c113909a05295408072c03749f1bbcc Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 19 Oct 2012 22:57:38 -0700 Subject: [PATCH 03/27] Finish simon_says to match spec --- week2/homework/simon_says.rb | 18 ++++++++++++++++++ 1 file changed, 18 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..2b5f099 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,18 @@ +module SimonSays + def echo(text) + text + end + def shout(text) + text.upcase + end + def repeat(text,times=2) + ("#{text} " * times).chop + end + def start_of_word(text,letters=1) + text[0,letters] + end + def first_word(text,words=1) + # Could also be done with a regex + text.split(" ").first + end +end \ No newline at end of file From 39f5f6867d1c4e77c23e7d8271c8cf43c05b91bf Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 22 Oct 2012 21:41:24 -0700 Subject: [PATCH 04/27] Finish homework --- week2/homework/questions.txt | 26 +++++++++++++++++++++----- week2/homework/simon_says.rb | 2 +- week2/homework/simon_says_spec.rb | 5 ++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 247547d..06825e0 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -2,8 +2,24 @@ Please Read The Chapters on: Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins -1. What is the difference between a Hash and an Array? -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? -5. What is the difference between a Module and a Class? +Q1. What is the difference between a Hash and an Array? + +A1. Hashes and arrays are both indexed collections of objects, however, while an array is indexed by non-negative integers, a hash +is indexed by other objects referred to as keys. Symbols are often used as keys. + +Q2. When would you use an Array over a Hash and vice versa? + +A2. Arrays are useful when you just need an ordered list or a collection of objects. Hashes are better for when you need to have a name or some +useful reference to these objects, such as for configuration variables or other settings. + +Q3. What is a module? Enumerable is a built in Ruby module, what is it? + +A3. A module is a portable set of methods that can be imported into a specific class. Enumerable is a module that allows you to add various collection iteration methods + +Q4. Can you inherit more than one thing in Ruby? How could you get around this problem? + +A4. No; you must use mixins. + +Q5. What is the difference between a Module and a Class? + +A5. Classes define a set of objects, an object cannot not be initialized using a module. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 2b5f099..6d4f266 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -6,7 +6,7 @@ def shout(text) text.upcase end def repeat(text,times=2) - ("#{text} " * times).chop + ("#{text} " * times).rstrip end def start_of_word(text,letters=1) text[0,letters] diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index a17ac8d..5bfba8f 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -8,7 +8,10 @@ it "should echo hello" do echo("hello").should == "hello" end - + it "should echo cheese" do + echo("cheese").should == "cheese" + end + it "should echo bye" do echo("bye").should == "bye" end From a855273763a603e0afe468e7472c2583f8afa1ac Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Sun, 28 Oct 2012 11:07:23 -0700 Subject: [PATCH 05/27] doop --- week2/homework/simon_says.rb | 66 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 956ef13..f0e5c84 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,41 +1,41 @@ module SimonSays - def echo(text) - text - end - def shout(text) - text.upcase - end - def repeat(text,times=2) - ("#{text} " * times).rstrip - end - def start_of_word(text,letters=1) - text[0,letters] - end - def first_word(text,words=1) - # Could also be done with a regex - text.split(" ").first - end + def echo(text) + text + end + def shout(text) + text.upcase + end + def repeat(text,times=2) + ("#{text} " * times).rstrip + end + def start_of_word(text,letters=1) + text[0,letters] + end + def first_word(text,words=1) + # Could also be done with a regex + text.split(" ").first + end end # UWE-Ruby Answers module SimonSays - def echo(st) - st - end - - def shout(st) - st.upcase - end + def echo(st) + st + end - def first_word(st) - st.split.first - end + def shout(st) + st.upcase + end - def start_of_word(st,i) - st[0...i] - end - - def repeat(st, t=2) - ([st]*t).join(' ') - end + def first_word(st) + st.split.first + end + + def start_of_word(st,i) + st[0...i] + end + + def repeat(st, t=2) + ([st]*t).join(' ') + end end From 70283905b928a0d9f72a00d255aa04c27164daca Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Sun, 28 Oct 2012 13:28:56 -0700 Subject: [PATCH 06/27] Spec --- week3/homework/calculator.rb | 15 +++++++++ week3/homework/calculator_spec.rb | 54 ++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index ede464b..49c1912 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,2 +1,17 @@ class Calculator + def sum(array) + return 0 if array.empty? + array.inject(:+) + end + def multiply(array) + return 0 if array.empty? + array.inject(:*) + end + def power(n1,n2) + n1**n2 + end + def factorial(n) + return 0 if n.zero? + (1..n).inject(:*) + end end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 08b81cb..ce8545c 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,7 +1,7 @@ require "#{File.dirname(__FILE__)}/calculator" describe Calculator do - + before do @calculator = Calculator.new end @@ -10,36 +10,54 @@ it "computes the sum of an empty array" do @calculator.sum([]).should == 0 end - + it "computes the sum of an array of one number" do @calculator.sum([7]).should == 7 end - + it "computes the sum of an array of two numbers" do @calculator.sum([7,11]).should == 18 end - + it "computes the sum of an array of many numbers" do @calculator.sum([1,3,5,7,9]).should == 25 end end - - # Once the above tests pass, + + # Once the above tests pass, # write tests and code for the following: - - it "multiplies two numbers" + describe "#multiply" do + it "multiplies two numbers" do + @calculator.multiply([5,6]).should == 30 + end + + it "multiplies an array of numbers" do + @calculator.multiply([5,6,10]).should == 300 + end + end + + describe "#power" do + it "raises one number to the power of another number" do + @calculator.power(5,6).should == 15625 + end + end - it "multiplies an array of numbers" - - it "raises one number to the power of another number" - # 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 a32a738f03086ba5dea2fbcbc7d82039d3254610 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 30 Oct 2012 18:01:23 -0700 Subject: [PATCH 07/27] Homework questions --- week3/homework/questions.txt | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index e9a9079..86db94d 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -4,9 +4,26 @@ Please Read: - Chapter 7 Regular Expressions - Chapter 22 The Ruby Language: basic types (symbols), variables and constants -1. What is a symbol? -2. What is the difference between a symbol and a string? -3. What is a block and how do I call a block? -4. How do I pass a block to a method? What is the method signature? -5. Where would you use regular expressions? +Q1. What is a symbol? +A1. A symbol is a constant that has a consistent name. + +Q2. What is the difference between a symbol and a string? + +A2. A symbol is a consistent object, that is guarenteed to be unique. A string is a changeable object. + +Q3. What is a block and how do I call a block? + +A3. A block of a chunk of code that contains the variables when it was initialized. You can call a block by `yield` +or Proc::call. + +Q4. How do I pass a block to a method? What is the method signature? + +A4. At the end of the method call between curly brackets, such as do_this_for_reals { block here }, which can then be called with yield. +If you'd rather pass in a proc, method_name(arg1, argv2, &proc) + +Q5. Where would you use regular expressions? + +A5. Where wouldn't you use a regular expression? They're useful to match patterns, +pull specific chunks of strings that match patterns out, and all sorts of other such +tidbits. Not to mention substitution! From 4768b2159d91eeab6b024a50c68f4c3dd01e24b6 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 2 Nov 2012 13:46:04 -0700 Subject: [PATCH 08/27] My timer --- week4/class_materials/timer_spec.rb | 37 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb index a6092f1..d621bd9 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -2,19 +2,30 @@ describe Timer do - it "should report the time difference" do - Time.stub(:now).and_return(0,3) - time_difference = Timer.time_code do - end - time_difference.should be_within(0.1).of(3.0) - end + it "should report the time difference" do + # The first time Time.now is run, return 0, every time thereafter, 3 + Time.stub(:now).and_return(0,3) + time_difference = Timer.time_code do + end + time_difference.should be_within(0.1).of(3.0) + end - it "should run our code" do - flag = false - Timer.time_code do - flag = true - end - flag.should be_true - end + it "should run our code" do + flag = false + Timer.time_code do + flag = true + end + flag.should be_true + end + it "should run our code multiple times" do + counter = 0 + result = Timer.time_code(17) {counter += 1} + counter.should eq 17 + end + it "should give the average time" do + Time.stub(:now).and_return(0,10) + result = Timer.time_code(10) { } + result.should be_within(0.1).of(1) + end end From 6766c6a562ca63551ba2971ef7da51f697b01e10 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 2 Nov 2012 13:46:21 -0700 Subject: [PATCH 09/27] Various class exercises --- RubyFall2012.sublime-project | 8 + RubyFall2012.sublime-workspace | 1083 ++++++++++++++++++++++++++++ week4/class_materials/OddNumber.rb | 17 + week4/class_materials/timer.rb | 9 + week4/exercises/worker.rb | 9 + 5 files changed, 1126 insertions(+) create mode 100644 RubyFall2012.sublime-project create mode 100644 RubyFall2012.sublime-workspace create mode 100644 week4/class_materials/OddNumber.rb create mode 100644 week4/class_materials/timer.rb create mode 100644 week4/exercises/worker.rb diff --git a/RubyFall2012.sublime-project b/RubyFall2012.sublime-project new file mode 100644 index 0000000..8c372fb --- /dev/null +++ b/RubyFall2012.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "/home/nikky/Repositories/github/RubyFall2012" + } + ] +} diff --git a/RubyFall2012.sublime-workspace b/RubyFall2012.sublime-workspace new file mode 100644 index 0000000..81170b3 --- /dev/null +++ b/RubyFall2012.sublime-workspace @@ -0,0 +1,1083 @@ +{ + "auto_complete": + { + "selected_items": + [ + [ + "core", + "core_stats" + ], + [ + "non", + "non_assign_stat_grid" + ], + [ + "female", + "female_first_names" + ], + [ + "male", + "male_first_names" + ], + [ + "AVAIL", + "AVAILABLE_RACES" + ], + [ + "get", + "get_with_array" + ], + [ + "list", + "list_characters" + ], + [ + "chara", + "characters" + ], + [ + "character", + "character_unique_traits" + ], + [ + "last", + "last_names" + ], + [ + "generate", + "generate_sex" + ], + [ + "select", + "select_race" + ], + [ + "err", + "error_check" + ], + [ + "en", + "element" + ] + ] + }, + "buffers": + [ + { + "file": "week3/exercises/monster.rb", + "settings": + { + "buffer_size": 310, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/named_thing.rb", + "settings": + { + "buffer_size": 167, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/vampire.rb", + "settings": + { + "buffer_size": 186, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/monsters.rb", + "settings": + { + "buffer_size": 830, + "line_ending": "Unix" + } + } + ], + "build_system": "", + "command_palette": + { + "height": 392.0, + "selected_items": + [ + [ + "beau", + "BeautifyRuby" + ], + [ + ":w", + ":w - Save" + ], + [ + "project", + "Project: Close" + ], + [ + ":u", + "Snippet: class .. < Test::Unit::TestCase .. end" + ], + [ + ":W", + ":w - Save" + ], + [ + "install", + "Package Control: Install Package" + ], + [ + "instll", + "Package Control: Install Package" + ], + [ + "in", + "Indentation: Reindent Lines" + ], + [ + "ann", + "Git: Toggle Annotations" + ], + [ + "anno", + "Git: Toggle Annotations" + ], + [ + "bea", + "BeautifyRuby" + ], + [ + "git stat", + "Git: Status" + ], + [ + "remove", + "Package Control: Remove Package" + ], + [ + "remo", + "Package Control: Remove Package" + ], + [ + "tidy", + "Tidy HTML" + ], + [ + "package", + "Package Control: Install Package" + ], + [ + "hg", + "SublimeHg: Open Menu" + ], + [ + "nex", + "SublimeLint: Next Error" + ], + [ + "next", + "SublimeLint: Next Error" + ], + [ + "lint", + "SublimeLint: Next Error" + ], + [ + "rem", + "Package Control: Remove Package" + ], + [ + "tid", + "Tidy HTML" + ], + [ + "inten", + "Indentation: Reindent Lines" + ], + [ + "tag", + "Tag: Auto-Format Tags on Document" + ], + [ + "zencoding", + "Zen Coding: Endcode/decode base64" + ], + [ + "zen coding", + "Set Syntax: ZenCoding" + ], + [ + "zen ", + "Zen Coding: Evaluate math expression" + ], + [ + "zen", + "Zen Coding: Zen as you type" + ], + [ + "html", + "Tidy HTML" + ], + [ + "sublime", + "SublimeLinter: Show Error List" + ], + [ + "package in", + "Package Control: Install Package" + ], + [ + "Package Control: ", + "Package Control: Discover Packages" + ], + [ + "inden", + "Indentation: Reindent Lines" + ], + [ + "indent", + "Indentation: Reindent Lines" + ], + [ + "ident", + "Indentation: Reindent Lines" + ], + [ + "hg ", + "SublimeHg: Open Menu" + ], + [ + "html ", + "Tidy HTML" + ], + [ + "intent", + "Indentation: Reindent Lines" + ], + [ + "rein", + "Indentation: Reindent Lines" + ], + [ + "open", + "View: Toggle Open Files in Side Bar" + ], + [ + "install pack", + "Package Control: Install Package" + ], + [ + "package re", + "Package Control: Remove Package" + ], + [ + "package install", + "Package Control: Install Package" + ], + [ + "sftp", + "SFTP: Browse Server…" + ], + [ + "package install", + "Package Control: Install Package" + ] + ], + "width": 593.0 + }, + "console": + { + "height": 241.0 + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "file_history": + [ + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/simon_says_spec.rb", + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/questions.txt", + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/simon_says.rb", + "/home/nikky/Repositories/www/choosing_cms.html", + "/home/nikky/Repositories/www/start.html", + "/home/nikky/Repositories/www/index.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Preferences.sublime-settings", + "/home/nikky/Nikky_Sim_0.00.00/source/nikky.c", + "/home/nikky/Nikky_Sim_0.00.00/source/vocab.c", + "/home/nikky/Nikky_Sim_0.00.00/source/ui.c", + "/home/nikky/Nikky_Sim_0.00.00/license.txt", + "/home/nikky/Nikky_Sim_0.00.00/source/build.sh", + "/home/nikky/Repositories/github/UW-Wordpress-Theme/index.php", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Automatic Backups.sublime-settings", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/BeautifyRuby/BeautifyRuby.sublime-settings", + "/home/nikky/Repositories/github/Mass-Email-o-nator/mailer3.rb", + "/home/nikky/Repositories/github/Mass-Email-o-nator/configuration.rb", + "/home/nikky/Repositories/github/Mass-Email-o-nator/configuration.sample.rb", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/Default/Preferences.sublime-settings", + "/home/nikky/Repositories/github/Mass-Email-o-nator/doop", + "/home/nikky/Repositories/github/staff-website/doop", + "/home/nikky/Repositories/www/access.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/SublimeLinter.sublime-settings", + "/home/nikky/Repositories/www/basic-group.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/README.md", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/SublimeLinter.sublime-settings", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/Default (Linux).sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/sublimelint/SublimeLint.sublime-settings", + "/home/nikky/Repositories/www/web-development-servers.html", + "/home/nikky/Repositories/www/mysql/mysql-admin.html", + "/home/nikky/Repositories/www/mysql/mysql-installed.html", + "/home/nikky/Repositories/www/www.sublime-project", + "/home/nikky/Repositories/www/mysql/index.html", + "/home/nikky/Repositories/www/using-servers.html", + "/home/nikky/Repositories/www/website-urls.html", + "/home/nikky/Repositories/www/mailing-lists.html", + "/home/nikky/Repositories/dnd-4e-character-generator/lessons_learned.md", + "/home/nikky/Repositories/dnd-4e-character-generator/class_test.rb", + "/home/nikky/Repositories/dnd-4e-character-generator/libGen.rb", + "/home/nikky/Repositories/dnd-4e-character-generator/array_object_test.rb", + "/home/nikky/Repositories/learn_c_hard_way/ex11.c", + "/home/nikky/Repositories/learn_c_hard_way/ex11", + "/home/nikky/Repositories/learn_c_hard_way/ex3.c", + "/home/nikky/Repositories/learning-ruby/mailer3/mailer3.rb", + "/home/nikky/Repositories/www/.wwwinstrc", + "/home/nikky/Repositories/www/publish.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/ZenCoding/Default.sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/HtmlTidy/HtmlTidy.sublime-settings", + "/home/nikky/Repositories/www/ssh.html", + "/home/nikky/Repositories/www/mysql/phpmyadmin.html", + "/home/nikky/Repositories/www/mysql/phpmyadmin-upgrade.html", + "/home/nikky/Repositories/www/phpmyadmin.html", + "/home/nikky/Repositories/www/phpmyadmin-upgrade.html", + "/home/nikky/Repositories/www/snippets/ssh_web_dev.html", + "/home/nikky/Repositories/www/mysql/mysql-security.html", + "/home/nikky/Repositories/www/mysql/mysql-desktop.html", + "/home/nikky/Repositories/www/mysql/mysql-client.html", + "/home/nikky/Repositories/www/mysql/mysql_backup.sh", + "/home/nikky/Repositories/learning-ruby/mailer3/configuration.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/libMailer3.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/do_mail.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/settings.yml", + "/home/nikky/Repositories/learning-ruby/mailer3/settings.yaml", + "/home/nikky/Repositories/www/mysql/mysql-script.html", + "/home/nikky/Repositories/www/mysql/mysql-lists.html", + "/home/nikky/Repositories/www/mysql/mysql-access-3.5.html", + "/home/nikky/Repositories/www/mysql/mysql-installed.old.html", + "/home/nikky/Dropbox/config/deploy.rb", + "/home/nikky/learning-ruby/mailer3/mailer3.rb", + "/tmp/sublime-sftp-browse-1344966977/nykida_net/home/nikky/.vimrc", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/sftp_servers/nikky_ovid", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/sftp_servers/nykida_net", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/Default/Default (Linux).sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Distraction Free.sublime-settings" + ], + "find": + { + "height": 36.0 + }, + "find_in_files": + { + "height": 93.0, + "where_history": + [ + "" + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + "cheese", + "mediawiki", + "nav-coll", + "dropdowns", + "Walker_Menu", + "uw_drop", + "menu", + "head", + "shared_netid", + "cc", + "message_parse_block", + " @message_parse_block", + "@message_parse_block", + "ruler", + "width", + "@config", + "$debug", + " $debug", + "$debug", + "debug", + "$debug", + "log", + "log_event", + "html", + "map", + "ruby", + "backing up", + "for", + "home pages", + "Individual", + "ossible", + "gets", + "generate_stats", + ".class", + "class", + "Elf", + "halfling", + "Elf", + "Tiefling", + "Dragonborn", + "Warrior", + "Scaffold_new_character", + "characters", + "male_names", + "unique_traits", + "load_class_stats", + "generate_stats", + "nil", + "btn-info", + "btn", + "large", + "primary", + "btn", + "fugu", + "Click for more information on development", + "strong", + "b>", + "Tutorial Introduction", + "Backing up your", + "@", + "div", + " (o) + @value <=> o.value + end +end diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb new file mode 100644 index 0000000..e5b03bd --- /dev/null +++ b/week4/class_materials/timer.rb @@ -0,0 +1,9 @@ +class Timer + def self.time_code(n=1,&my_code) + # or yield + start_time = Time.now + n.times { my_code.call } + end_time = Time.now + (end_time - start_time) / n.to_f + end +end \ No newline at end of file diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..e48f423 --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,9 @@ +class Worker + def self.work(n=1) + result = '' + n.times do + result = yield + end + result + end +end From bcfab7596be76be6a4e36c7c8f431331612ffad7 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 5 Nov 2012 19:52:32 -0800 Subject: [PATCH 10/27] Finish questions. --- week4/homework/questions.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..f4bf3de 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,29 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + +Ruby reads files line-by-line using an IO object. + 2. How would you output "Hello World!" to a file called my_output.txt? + +File.open("my_output.txt","w") do |f| + file.puts "Hello World!" +end + 3. What is the Directory class and what is it used for? + +(I assume this is the Dir class). Dir allows you to interact with directories on the system +that is running the Ruby code. You can change directories, list contents, and other similar +directory-related functions. + 4. What is an IO object? + +An IO object allows you to do much more with IO than puts/gets allows. Essentially, it allows two-way communication +with another resource. You can read an write from files, as +well as interact with other servers and protocols across TCP/IP sockets. + 5. What is rake and what is it used for? What is a rake task? + +Rake is Make... but in Ruby. You can use it to automate certain "tasks" such as cleaning up +test instances, files, and automating other functionality. A task is a certain activity that you define, such as +"clean." From cb18f933364ccbad2085dbf8593783fcf8ded664 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 9 Nov 2012 23:27:02 -0800 Subject: [PATCH 11/27] merge --- week5/exercises/Rakefile | 23 ++++++++++++++++++ week5/exercises/names | 50 +++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index 68a8f60..36db327 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -1,3 +1,26 @@ task :test do puts "Hello World!" end +task :list_names do + File.open("names","r") do |n| + n.each do |l| + puts l + end + end +end +task :create_directory do + Dir.mkdir("class") unless Dir.exists?("class") +end +task :populate_directory => [:create_directory] do + parse_name_file("names") + +end + +def parse_name_file(file) + f = File.open(file,"r") + Dir.cd("class") + f.readlines.each do |n| + Dir.mkdir(n) + end +end + diff --git a/week5/exercises/names b/week5/exercises/names index 9eec0ec..552edd2 100644 --- a/week5/exercises/names +++ b/week5/exercises/names @@ -1,35 +1,33 @@ -Ben +name +Brian +Renee +Yan +Nell BrianT -BrianWard -BryanWilliams -Cheri -Chris B -ChristopherF -ChristopherT +Emily Danny -Dimitry +Chris +ChrisF +Mallory +Greg +Tim Eddie -Emily -Gregory +Steven Josh -Karen -Kat +Nikky +ChrisB Kelli +Ben +Cheri +Strand Kris -Mallory -Nell -NicholasP -Nicole -Nikky -Peter -Price -Renée -Ryan Santy +Nicholas +Karen +Bryan Serene -Sol -Steven -Strand -Timothy +Peter +Price Will -Yan +Nicole +Sol From 929d9851f414104903c9f320cc046a8e16af07d1 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 27 Nov 2012 17:46:49 -0800 Subject: [PATCH 12/27] week7 homework --- week6/derp_deep_gem | 1 + .../features/step_definitions/converter.rb | 2 +- .../features/step_definitions/puppy.rb | 20 +++++++++++ .../features/step_definitions/puppy_steps.rb | 35 +++++++++++++++++++ .../features/step_definitions/pirate.rb | 16 +++++++++ .../features/step_definitions/pirate_steps.rb | 20 +++++++++++ week7/homework/questions.txt | 25 +++++++++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 160000 week6/derp_deep_gem create mode 100644 week7/exercises/features/step_definitions/puppy.rb create mode 100644 week7/exercises/features/step_definitions/puppy_steps.rb create mode 100644 week7/homework/features/step_definitions/pirate.rb create mode 100644 week7/homework/features/step_definitions/pirate_steps.rb diff --git a/week6/derp_deep_gem b/week6/derp_deep_gem new file mode 160000 index 0000000..975b788 --- /dev/null +++ b/week6/derp_deep_gem @@ -0,0 +1 @@ +Subproject commit 975b7886d9986fa41234bf9ded9fe01b1fb69f41 diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb index 4cb1264..b3dee08 100644 --- a/week7/exercises/features/step_definitions/converter.rb +++ b/week7/exercises/features/step_definitions/converter.rb @@ -12,6 +12,6 @@ def convert end def Fahrenheit_convertion - (((@value - 32.0) /5.0) * 9.0).round(1) + (((@value - 32.0) /9.0) * 5.0).round(1) end end \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/puppy.rb b/week7/exercises/features/step_definitions/puppy.rb new file mode 100644 index 0000000..bdc86c7 --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy.rb @@ -0,0 +1,20 @@ +class Puppy + def name=(arg1) + @name = arg1 + end + def pet + wag_tail + end + def wag_tail + "Yayyyy!" + end + def ring_bell + wag_tail + end + def take_dog_out + @dog_walked = true + end + def pee? + @dog_walked? false : true + end +end \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/puppy_steps.rb b/week7/exercises/features/step_definitions/puppy_steps.rb new file mode 100644 index 0000000..5f1d96e --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy_steps.rb @@ -0,0 +1,35 @@ +Given /^we have a puppy$/ do + @puppy = Puppy.new +end + +Given /^its name is Fred$/ do + @puppy.name = "Fred" +end + +When /^we pet the puppy$/ do + @result = @puppy.pet +end + +Then /^the puppy wags its tail$/ do + @result.should == "Yayyyy!" +end + +Given /^its name is Bella$/ do + @puppy.name = "Bella" +end + +When /^we ring the bell$/ do + @result = @puppy.ring_bell +end + +When /^it wags its tail$/ do + @result.should == "Yayyyy!" +end + +Then /^we must take it out$/ do + @puppy.take_dog_out.should == true +end + +Then /^then it will not pee on the floor$/ do + @puppy.pee?.should == false +end diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..70c8329 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,16 @@ +class Pirate + def say(str1) + @phrase = str1 + end + def translate + @phrases = { 'Hello Friend' => 'Ahoy Matey' } + #false if @phrase.nil? + @result = @phrases.fetch(@phrase, "derp") + end + def say_result + @result + end + def end_convo + "Shiber Me Timbers You Scurvey Dogs!!" + end +end diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb new file mode 100644 index 0000000..dd50054 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -0,0 +1,20 @@ + +Gangway /^I have a PirateTranslator$/ do + @parrot = Pirate.new +end + +Blimey /^I say 'Hello Friend'$/ do + @parrot.say("Hello Friend").should == "Hello Friend" +end + +Blimey /^I hit translate$/ do + @parrot.translate +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @parrot.say_result.should == "Ahoy Matey" +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @parrot.end_convo.should == 'Shiber Me Timbers You Scurvey Dogs!!' +end diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..0daa578 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,32 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +method_missing is method that returns a boolean depending on weather an object will +respond to a certain method. It can be used to detect if a specific object will work for +whatever methods you want to run (for instance, instead of checking to see if something is an array, instead see if it responds to .each?, which means you can use a file object as well.) + + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Eigenclass is synonymous to 'singleton class' in Ruby, and is an anonymous class. They contain singleton methods for a specific object. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +DuckTyping is good for cases where you don't need a specific object "class" but rather +a supported method. For instance, instead of requiring an array object, you can instead check to see if it supports something like .each, and then you can support arrays, hashes, I/O, etc. etc. They're more flexible and refactoring/testing is easier. + + 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 is acted upon a specific class without instantiating it first; such as File.open. + +Instance methods are ran against instantiated objects, like File.new (class method) and then File.each (instance method). + +`instance_eval` and `class_eval` both set `self` to whatever object you so choose, run the code inside the block, and then reset `self`. + +However, `class_eval` sets an instance method, whereas instance_eval will set a class_method. So it's kind of backwards opposite day. + 5. What is the difference between a singleton class and a singleton method? + +A singleton method is a per-object method. A singleton class is the anonymous class that holds singleton methods for a particular object. From 501a8689f009ae3d4b783f807e1150632c570cb7 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 27 Nov 2012 19:47:54 -0800 Subject: [PATCH 13/27] Class examples --- week8/class_materials/meta_example.rb | 46 +++++++++++++++++++++++++++ week8/exercises/couch.rb | 33 ++++++++++--------- 2 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 week8/class_materials/meta_example.rb diff --git a/week8/class_materials/meta_example.rb b/week8/class_materials/meta_example.rb new file mode 100644 index 0000000..3b16a9f --- /dev/null +++ b/week8/class_materials/meta_example.rb @@ -0,0 +1,46 @@ +class Book +end + + +b = Book.new +b1 = Book.new + +b.instance_eval do + def cheese + puts "cheesy" + end +end + +b.cheese +# b1.cheese <= this should error out +class Object + def call_chain + "self" + end +end + +module NamedThing + def call_chain + "#{NamedThing.super}" + end +end + +module Speaker + def call_chain + "#{self}.#{super}" + end +end + +class Animal + def call_chain + "#{self}.#{super}" + end +end + +class Person < Animal + include NamedThing + include Speaker + def call_chain + "#{self}.#{super}" + end +end \ No newline at end of file diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..999d220 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -1,20 +1,23 @@ class Couch - def initialize(pillows, cushions) - @pillows = pillows - @cushions = cushions - end + def initialize(pillows, cushions) + @pillows = pillows + @cushions = cushions + end - def pillow_colors - @pillows.join(", ") - end + # def pillow_colors + # @pillows.join(", ") + # end - def cushion_colors - @cushions.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 + [:pillows, :cushions].each do |s| + define_method("how_many_#{s}") do + instance_variable_get("@#{s}").count + end + define_method("#{s.to_s.chomp('s')}_colors") do + instance_variable_get("@#{s}").join(', ') + end + end end From 8a933e1e823f7cf7cb5c2a512aa26c1f3faf506e Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 27 Nov 2012 19:48:15 -0800 Subject: [PATCH 14/27] Remove --- week6/derp_deep_gem | 1 - 1 file changed, 1 deletion(-) delete mode 160000 week6/derp_deep_gem diff --git a/week6/derp_deep_gem b/week6/derp_deep_gem deleted file mode 160000 index 975b788..0000000 --- a/week6/derp_deep_gem +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 975b7886d9986fa41234bf9ded9fe01b1fb69f41 From 5af9167bf4480c4ead4772eed27e4a34b6313acc Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Wed, 28 Nov 2012 10:02:39 -0800 Subject: [PATCH 15/27] In-class exercise --- week8/exercises/couch.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index 999d220..eb06744 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -11,7 +11,12 @@ def initialize(pillows, cushions) # def cushion_colors # @cushions.join(", ") # end - + def method_missing(method, *args, &block) + puts "You called #{method} with #{args.join(' ')}" + end + def to_str + "I am a couch" + end [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count From 0501afcb9d6d3855e632bf9e188d5facc391a955 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Wed, 28 Nov 2012 10:04:51 -0800 Subject: [PATCH 16/27] Clarify --- week7/homework/features/step_definitions/pirate.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb index 8c0dc9a..3411649 100644 --- a/week7/homework/features/step_definitions/pirate.rb +++ b/week7/homework/features/step_definitions/pirate.rb @@ -1,4 +1,4 @@ -<<<<<<< HEAD +# nikky version class Pirate def say(str1) @phrase = str1 @@ -15,7 +15,7 @@ def end_convo "Shiber Me Timbers You Scurvey Dogs!!" end end -======= +# class answer class PirateTranslator PIRATE_WORDS = { "Hello Friend" => "Ahoy Matey" @@ -33,4 +33,3 @@ def lookup_pirate(str) PIRATE_WORDS[str] end end ->>>>>>> 4b2958513d06a8a3cac07f3785ec431b77644ba7 From 45ea5abed4c34645867b0a928749c48906b5f261 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 30 Nov 2012 18:56:48 -0800 Subject: [PATCH 17/27] Incomplete homework --- week7/homework/features/step_definitions/pirate_steps.rb | 6 +++--- .../homework/features/step_definitions/tic-tac-toe-steps.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index 603ffd2..5b00215 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,4 +1,4 @@ -<<<<<<< HEAD +# class answers Gangway /^I have a PirateTranslator$/ do @parrot = Pirate.new @@ -18,7 +18,8 @@ Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do @parrot.end_convo.should == 'Shiber Me Timbers You Scurvey Dogs!!' -======= +#mine +end Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new end @@ -37,5 +38,4 @@ Letgoandhaul /^it also prints '(.+)'$/ do |arg| @result.split("\n ").last.should == arg ->>>>>>> 4b2958513d06a8a3cac07f3785ec431b77644ba7 end 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 b353120..d00e957 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| From 8746de710411cec189138d50b75d1e333ff81d1a Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Fri, 30 Nov 2012 18:58:17 -0800 Subject: [PATCH 18/27] Derp --- .../features/step_definitions/tic-tac-toe.rb | 114 ++++++++++++++++++ 1 file changed, 114 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..03f3656 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,114 @@ +class TicTacToe + attr_accessor :player, :board + attr_reader :player_symbol, :computer_symbol, :current_player, :next_player, :over + SYMBOLS = [:X, :O] + def initialize(first_player=nil,player_symbol=nil) + @board = { + A1: nil, A2: nil, A3: nil, + B1: nil, B2: nil, B3: nil, + C1: nil, C2: nil, C3: nil + } + if first_player.nil? + random_start + else + set_first_player(first_player) + end + if player_symbol.nil? + random_symbol + else + # set_symbol(@current_player,first_player_symbol) + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS - [@player_symbol])[0] + end + end + def player=(player) + @player = player + end + def random_symbol + if rand(0..1).zero? + @player_symbol = SYMBOLS[0] + @computer_symbol = SYMBOLS[1] + else + @player_symbol = SYMBOLS[1] + @computer_symbol = SYMBOLS[0] + end + end + def random_start + if rand(0..1).zero? + set_first_player(:player) + else + set_first_player(:computer) + end + end + def welcome_player + if @player.nil? + "Welcome Anonymous Player" + else + "Welcome #{@player}" + end + end + + def over? + end + def current_player + if @current_player == :computer + "Computer" + elsif @player != nil + @player + else + "Player" + end + end + def next_turn + @current_player = :computer if @current_player == :player + @current_player = :player if current_player == :computer + end + def set_first_player(p) + @current_player = p + end + def set_symbol(p,s) + #set_ionstance_method("@#{p}") + end + def indicate_player_turn + if current_player == :computer + "Computer's Move:" + elsif player != nil + "#{player}'s Move:" + else + "Your Move:" + end + end + def get_player_move + move = gets.chomp + @board[move] = @player_symbol + end + def open_spots + @board.select { |k,v| v.nil?} + end + def computer_move + computer_square = open_spots.keys.sample + @board[computer_square] = @computer_symbol + computer_square + end + def current_state + output = '' + @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output +end +end \ No newline at end of file From eb96911f1febbf46f3e506716781ac3cab612003 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Sat, 1 Dec 2012 20:34:25 -0800 Subject: [PATCH 19/27] still flawed, but on the way --- .../features/step_definitions/tic-tac-toe.rb | 242 ++++++++++-------- week7/homework/play_game.rb | 2 +- 2 files changed, 132 insertions(+), 112 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 03f3656..ce67919 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,114 +1,134 @@ class TicTacToe - attr_accessor :player, :board - attr_reader :player_symbol, :computer_symbol, :current_player, :next_player, :over - SYMBOLS = [:X, :O] - def initialize(first_player=nil,player_symbol=nil) - @board = { - A1: nil, A2: nil, A3: nil, - B1: nil, B2: nil, B3: nil, - C1: nil, C2: nil, C3: nil - } - if first_player.nil? - random_start - else - set_first_player(first_player) - end - if player_symbol.nil? - random_symbol - else - # set_symbol(@current_player,first_player_symbol) - @player_symbol = player_symbol - @computer_symbol = (SYMBOLS - [@player_symbol])[0] - end - end - def player=(player) - @player = player - end - def random_symbol - if rand(0..1).zero? - @player_symbol = SYMBOLS[0] - @computer_symbol = SYMBOLS[1] - else - @player_symbol = SYMBOLS[1] - @computer_symbol = SYMBOLS[0] - end - end - def random_start - if rand(0..1).zero? - set_first_player(:player) - else - set_first_player(:computer) - end - end - def welcome_player - if @player.nil? - "Welcome Anonymous Player" - else - "Welcome #{@player}" - end - end + attr_accessor :player, :board, :over + attr_reader :player_symbol, :player_move, :computer_symbol, :current_player, :next_player, :over + SYMBOLS = [:X, :O] + PLAYERS = [:player, :computer] + def initialize(first_player=nil,player_symbol=nil) + @board = { + A1: nil, A2: nil, A3: nil, + B1: nil, B2: nil, B3: nil, + C1: nil, C2: nil, C3: nil + } + if first_player.nil? + random_start + else + set_first_player(first_player) + end + if player_symbol.nil? + random_symbol + else + # set_symbol(@current_player,first_player_symbol) + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS - [@player_symbol])[0] + end + @player = "Anonymous" + end + def player=(player) + @player = player + end + def random_symbol + if rand(0..1).zero? + @player_symbol = SYMBOLS[0] + @computer_symbol = SYMBOLS[1] + else + @player_symbol = SYMBOLS[1] + @computer_symbol = SYMBOLS[0] + end + end + def random_start + if rand(0..1).zero? + set_first_player(:player) + else + set_first_player(:computer) + end + end + def welcome_player + "Welcome #{@player}" + end - def over? - end - def current_player - if @current_player == :computer - "Computer" - elsif @player != nil - @player - else - "Player" - end - end - def next_turn - @current_player = :computer if @current_player == :player - @current_player = :player if current_player == :computer - end - def set_first_player(p) - @current_player = p - end - def set_symbol(p,s) - #set_ionstance_method("@#{p}") - end - def indicate_player_turn - if current_player == :computer - "Computer's Move:" - elsif player != nil - "#{player}'s Move:" - else - "Your Move:" - end - end - def get_player_move - move = gets.chomp - @board[move] = @player_symbol - end - def open_spots - @board.select { |k,v| v.nil?} - end - def computer_move - computer_square = open_spots.keys.sample - @board[computer_square] = @computer_symbol - computer_square - end - def current_state - output = '' - @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output + def over? + true if draw? || player_won != nil + false + end + def current_player + if @current_player == :computer + "Computer" + else + @player + end + end + def next_turn + @current_player = (PLAYERS - [@current_player])[0] + end + def set_first_player(p) + @current_player = p + end + def set_symbol(p,s) + #set_ionstance_method("@#{p}") + end + def indicate_player_turn + if current_player == :computer + puts "Computer's Move:" + else + puts "#{player}'s Move:" + end + end + def get_player_move + @player_entry = gets.chomp.to_sym + end + def player_move + get_player_move + @board[@player_entry] = @player_symbol + next_turn + @player_entry + end + def open_spots + @board.select { |k,v| v.nil?} + end + def computer_move + computer_square = open_spots.keys.sample + @board[computer_square.to_sym] = @computer_symbol + #computer_square + next_turn + computer_square + end + def current_state + output = '' + @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output + end + def determine_winner + @over = 1 + end + def validate_move? + # Check to see if a player move is valid... + end + def spots_open? + if @board.select {|k,v| v.nil?}.empty? + false + else + true + end + end + def draw? + true if spots_open? == false + end + def player_won + end end -end \ No newline at end of file diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -8,7 +8,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 27f807e7d17a50b3e69bad7b87606f669aeb0a7f Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 08:02:45 -0800 Subject: [PATCH 20/27] Refactor --- .../features/step_definitions/tic-tac-toe.rb | 136 ++--------------- week7/homework/tic-tac-toe_old.rb | 138 ++++++++++++++++++ 2 files changed, 150 insertions(+), 124 deletions(-) create mode 100644 week7/homework/tic-tac-toe_old.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index ce67919..5356124 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,134 +1,22 @@ +# re-do this... a.rotate! to keep track of who is who? a.rotate![0] class TicTacToe - attr_accessor :player, :board, :over - attr_reader :player_symbol, :player_move, :computer_symbol, :current_player, :next_player, :over + attr_accessor :player SYMBOLS = [:X, :O] PLAYERS = [:player, :computer] - def initialize(first_player=nil,player_symbol=nil) - @board = { - A1: nil, A2: nil, A3: nil, - B1: nil, B2: nil, B3: nil, - C1: nil, C2: nil, C3: nil - } - if first_player.nil? - random_start - else - set_first_player(first_player) - end - if player_symbol.nil? - random_symbol - else - # set_symbol(@current_player,first_player_symbol) - @player_symbol = player_symbol - @computer_symbol = (SYMBOLS - [@player_symbol])[0] - end - @player = "Anonymous" - end - def player=(player) - @player = player - end - def random_symbol - if rand(0..1).zero? - @player_symbol = SYMBOLS[0] - @computer_symbol = SYMBOLS[1] - else - @player_symbol = SYMBOLS[1] - @computer_symbol = SYMBOLS[0] - end - end - def random_start - if rand(0..1).zero? - set_first_player(:player) - else - set_first_player(:computer) - end - end + @board = { + A1: " ", A2: " ", A3: " ", + B1: " ", B2: " ", B3: " ", + C1: " ", C2: " ", C3: " " + } def welcome_player - "Welcome #{@player}" + "Welcome #{get_player_reference}" end - - def over? - true if draw? || player_won != nil - false - end - def current_player - if @current_player == :computer - "Computer" + # This method returns what we call the player. + def get_player_reference + if @player.nil? + "Anonymous" else @player end end - def next_turn - @current_player = (PLAYERS - [@current_player])[0] - end - def set_first_player(p) - @current_player = p - end - def set_symbol(p,s) - #set_ionstance_method("@#{p}") - end - def indicate_player_turn - if current_player == :computer - puts "Computer's Move:" - else - puts "#{player}'s Move:" - end - end - def get_player_move - @player_entry = gets.chomp.to_sym - end - def player_move - get_player_move - @board[@player_entry] = @player_symbol - next_turn - @player_entry - end - def open_spots - @board.select { |k,v| v.nil?} - end - def computer_move - computer_square = open_spots.keys.sample - @board[computer_square.to_sym] = @computer_symbol - #computer_square - next_turn - computer_square - end - def current_state - output = '' - @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output - end - def determine_winner - @over = 1 - end - def validate_move? - # Check to see if a player move is valid... - end - def spots_open? - if @board.select {|k,v| v.nil?}.empty? - false - else - true - end - end - def draw? - true if spots_open? == false - end - def player_won - end end diff --git a/week7/homework/tic-tac-toe_old.rb b/week7/homework/tic-tac-toe_old.rb new file mode 100644 index 0000000..8e54cf9 --- /dev/null +++ b/week7/homework/tic-tac-toe_old.rb @@ -0,0 +1,138 @@ +# re-do this... a.rotate! to keep track of who is who? a.rotate![0] + + +class TicTacToe + attr_accessor :player, :board, :over + attr_reader :player_symbol, :player_entry, :player_move, :computer_symbol, :over + SYMBOLS = [:X, :O] + PLAYERS = [@player, "Computer"] + def initialize(first_player=nil,player_symbol=nil) + @board = { + A1: nil, A2: nil, A3: nil, + B1: nil, B2: nil, B3: nil, + C1: nil, C2: nil, C3: nil + } + if first_player.nil? + random_start + else + @current_player = first_player + end + if player_symbol.nil? + random_symbol + else + # set_symbol(@current_player,first_player_symbol) + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS - [@player_symbol]).first + end + @player = "Anonymous" + end + def player=(player) + @player = player + end + def random_symbol + if rand(0..1).zero? + @player_symbol = SYMBOLS[0] + @computer_symbol = SYMBOLS[1] + else + @player_symbol = SYMBOLS[1] + @computer_symbol = SYMBOLS[0] + end + end + def random_start + if rand(0..1).zero? + @current_player = :player + else + @current_player = :computer + end + end + def welcome_player + "Welcome #{@player}" + end + + def over? + true if draw? || player_won != nil + false + end + def current_player + if @current_player == :computer + "Computer" + else + @player + end + end + def next_turn + #@current_player = (["Computer",@player]-[@current_player])[0] + @current_player = (PLAYERS - [@current_player])[0] + end + def set_first_player(p) + @current_player = p + end + def set_symbol(p,s) + #set_ionstance_method("@#{p}") + end + def indicate_player_turn + if current_player == :computer + puts "Computer's Move:" + else + puts "#{player}'s Move:" + end + end + def get_player_move + @player_entry = gets.chomp.to_sym + end + def player_move + get_player_move + @board[@player_entry] = @player_symbol + next_turn + @player_entry.to_sym + end + def open_spots + @board.select { |k,v| v.nil?} + end + def computer_move + computer_square = open_spots.keys.sample + @board[computer_square.to_sym] = @computer_symbol + #computer_square + next_turn + computer_square + end + def current_state + output = '' + @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| + output << v.to_s unless v.nil? + output << " " if v.nil? + output << " | " + end + output + end + def determine_winner + @over = 1 + end + def validate_move? + # Check to see if a player move is valid... + end + def spots_open? + if @board.select {|k,v| v.nil?}.empty? + false + else + true + end + end + def draw? + true if spots_open? == false + end + def player_won + end +end From 069c605fa53aa848afe82b5b2f684d9dfb6a0777 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 17:31:17 -0800 Subject: [PATCH 21/27] Commitin' --- .../features/step_definitions/tic-tac-toe.rb | 137 +++++++++++++++++- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 5356124..c23d2df 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,16 +1,34 @@ # re-do this... a.rotate! to keep track of who is who? a.rotate![0] class TicTacToe - attr_accessor :player + attr_accessor :player, :player_symbol, :computer_symbol, :board, :player_won, :computer_won SYMBOLS = [:X, :O] - PLAYERS = [:player, :computer] - @board = { + PLAYERS = [:computer, :player] + def initialize(first_player=nil, player_symbol=nil) + @current_player = nil + @players = [:player, :computer] + @board = { A1: " ", A2: " ", A3: " ", B1: " ", B2: " ", B3: " ", C1: " ", C2: " ", C3: " " - } + } + if first_player != nil + @current_player = first_player + else + random_first_turn + end + if player_symbol != nil + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS-[@player_symbol])[0] + else + random_symbols + end + end def welcome_player "Welcome #{get_player_reference}" end + def indicate_player_turn + puts "#{get_player_reference}'s Move:" + end # This method returns what we call the player. def get_player_reference if @player.nil? @@ -19,4 +37,115 @@ def get_player_reference @player end end + def random_first_turn + @current_player = PLAYERS.shuffle[0] + end + def random_symbols + shuffled_symbols = SYMBOLS.shuffle + @player_symbol, @computer_symbol = shuffled_symbols[0], shuffled_symbols[1] + end + def current_player + if @current_player == :player + get_player_reference + else + "Computer" + end + end + def get_player_move + gets.chomp + end + def open_spots + @board.select { |k,v| v == " "} + end + def computer_move + move = open_spots.keys.sample + process_move(:computer, move) + end + def player_move + move = 'false' + until validate_move?(move.to_sym) + move = get_player_move.to_sym + end + process_move(:player,move) + end + + # This validates a move, registers it, and flips the next turn bit + def process_move(player, move) + return 1 unless validate_move?(move) + @board[move] = @computer_symbol if player == :computer + @board[move] = @player_symbol if player == :player + next_player + move + end + # This checks to see if the move falls upon an open spot + def validate_move?(move) + return true if open_spots.include?(move) + false + end + def next_player + end + def current_state + output = '' + @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output + end + def determine_winner + if @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != nil + victory = true + winner_symbol = @board[:A1] + elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != nil + victory = true + winner_symbol = @board[:B1] + elsif @board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C1] != nil + victory = true + winner_symbol = @board[:C1] + elsif @board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:A1] != nil + victory = true + winner_symbol = @board[:A1] + elsif @board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:A2] != nil + victory = true + winner_symbol = @board[:A2] + elsif @board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:A3] != nil + victory = true + winner_symbol = @board[:A3] + elsif @board[:A1] == @board[:B2] && @board[:B2] == @board[:C3] && @board[:A1] != nil + victory = true + winner_symbol = @board[:A1] + elsif @board[:A3] == @board[:B2] && @board[:B2] == @board[:C1] && @board[:A3] != nil + victory = true + winner_symbol = @board[:A3] + else + victory = false + end + if victory == true && winner_symbol == @player_symbol + @player_won = true + return true + elsif victory == true && winner_symbol == @computer_symbol + @computer_won = true + return true + end + victory + end + def spots_open? + return true unless open_spots.empty? + false + end + def player_won? + end end From 008d4e1994a56b901e7b96945c71229921bcef08 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 19:10:09 -0800 Subject: [PATCH 22/27] almost thar --- .../features/step_definitions/tic-tac-toe.rb | 200 ++++++++++-------- 1 file changed, 110 insertions(+), 90 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index c23d2df..7a25972 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -4,30 +4,30 @@ class TicTacToe SYMBOLS = [:X, :O] PLAYERS = [:computer, :player] def initialize(first_player=nil, player_symbol=nil) - @current_player = nil - @players = [:player, :computer] - @board = { - A1: " ", A2: " ", A3: " ", - B1: " ", B2: " ", B3: " ", - C1: " ", C2: " ", C3: " " - } - if first_player != nil - @current_player = first_player - else - random_first_turn - end - if player_symbol != nil - @player_symbol = player_symbol - @computer_symbol = (SYMBOLS-[@player_symbol])[0] - else - random_symbols - end + @current_player = nil + @players = [:player, :computer] + @board = { + A1: " ", A2: " ", A3: " ", + B1: " ", B2: " ", B3: " ", + C1: " ", C2: " ", C3: " " + } + if first_player != nil + @current_player = first_player + else + random_first_turn + end + if player_symbol != nil + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS-[@player_symbol])[0] + else + random_symbols + end end def welcome_player "Welcome #{get_player_reference}" end def indicate_player_turn - puts "#{get_player_reference}'s Move:" + puts "#{get_player_reference}'s Move:" end # This method returns what we call the player. def get_player_reference @@ -38,54 +38,54 @@ def get_player_reference end end def random_first_turn - @current_player = PLAYERS.shuffle[0] + @current_player = PLAYERS.shuffle[0] end def random_symbols - shuffled_symbols = SYMBOLS.shuffle - @player_symbol, @computer_symbol = shuffled_symbols[0], shuffled_symbols[1] + shuffled_symbols = SYMBOLS.shuffle + @player_symbol, @computer_symbol = shuffled_symbols[0], shuffled_symbols[1] end def current_player - if @current_player == :player - get_player_reference - else - "Computer" - end + if @current_player == :player + get_player_reference + else + "Computer" + end end def get_player_move - gets.chomp + gets.chomp end def open_spots @board.select { |k,v| v == " "} end def computer_move - move = open_spots.keys.sample - process_move(:computer, move) + move = open_spots.keys.sample + process_move(:computer, move) end def player_move - move = 'false' - until validate_move?(move.to_sym) - move = get_player_move.to_sym - end - process_move(:player,move) + move = 'false' + until validate_move?(move.to_sym) + move = get_player_move.to_sym + end + process_move(:player,move) end - # This validates a move, registers it, and flips the next turn bit - def process_move(player, move) - return 1 unless validate_move?(move) - @board[move] = @computer_symbol if player == :computer - @board[move] = @player_symbol if player == :player - next_player - move - end - # This checks to see if the move falls upon an open spot - def validate_move?(move) - return true if open_spots.include?(move) - false - end - def next_player - end - def current_state - output = '' + # This validates a move, registers it, and flips the next turn bit + def process_move(player, move) + return 1 unless validate_move?(move) + @board[move] = @computer_symbol if player == :computer + @board[move] = @player_symbol if player == :player + next_player + move + end + # This checks to see if the move falls upon an open spot + def validate_move?(move) + return true if open_spots.include?(move) + false + end + def next_player + end + def current_state + output = '' @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| output << v.to_s unless v == " " output << " " if v == " " @@ -106,46 +106,66 @@ def current_state output end def determine_winner - if @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != nil - victory = true - winner_symbol = @board[:A1] - elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != nil - victory = true - winner_symbol = @board[:B1] - elsif @board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C1] != nil - victory = true - winner_symbol = @board[:C1] - elsif @board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:A1] != nil - victory = true - winner_symbol = @board[:A1] - elsif @board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:A2] != nil - victory = true - winner_symbol = @board[:A2] - elsif @board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:A3] != nil - victory = true - winner_symbol = @board[:A3] - elsif @board[:A1] == @board[:B2] && @board[:B2] == @board[:C3] && @board[:A1] != nil - victory = true - winner_symbol = @board[:A1] - elsif @board[:A3] == @board[:B2] && @board[:B2] == @board[:C1] && @board[:A3] != nil - victory = true - winner_symbol = @board[:A3] - else - victory = false - end - if victory == true && winner_symbol == @player_symbol - @player_won = true - return true - elsif victory == true && winner_symbol == @computer_symbol - @computer_won = true - return true - end - victory + victory = false + @player_won = false + @computer_won = false + if @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != " " + winner_symbol = @board[:A1] + elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != " " + victory = true + winner_symbol = @board[:B1] + elsif @board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C1] != " " + victory = true + winner_symbol = @board[:C1] + elsif @board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:A1] != " " + victory = true + winner_symbol = @board[:A1] + elsif @board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:A2] != " " + victory = true + winner_symbol = @board[:A2] + elsif @board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:A3] != " " + victory = true + winner_symbol = @board[:A3] + elsif @board[:A1] == @board[:B2] && @board[:B2] == @board[:C3] && @board[:A1] != " " + victory = true + winner_symbol = @board[:A1] + elsif @board[:A3] == @board[:B2] && @board[:B2] == @board[:C1] && @board[:A3] != " " + victory = true + winner_symbol = @board[:A3] + elsif open_spots.empty? + draw + else + victory = false + end + if victory == true && winner_symbol == @player_symbol + @player_won = true + return true + elsif victory == true && winner_symbol == @computer_symbol + @computer_won = true + return true + end + victory + end + def player_won? + return true if @player_won + false + end + def computer_won? + return true if @computer_won + false end def spots_open? - return true unless open_spots.empty? - false + return true unless open_spots.empty? + false end - def player_won? + def over? + return true if player_won? || computer_won? + false + end + def draw + @draw = true + end + def draw? + true if @draw end end From bae80bfde900511762d4a8f512053ab27bd2e91f Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 19:11:02 -0800 Subject: [PATCH 23/27] done w/ tic-tac-toe v1 --- week7/homework/features/step_definitions/tic-tac-toe.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 7a25972..14a67ce 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -159,7 +159,7 @@ def spots_open? false end def over? - return true if player_won? || computer_won? + return true if player_won? || computer_won? || draw? false end def draw From b26b878fa40b552ec2c9e325a17f9b6b8647feff Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 20:45:37 -0800 Subject: [PATCH 24/27] almost there --- .../features/step_definitions/tic-tac-toe.rb | 3 + week7/homework/play_game.rb | 4 + week7/homework/tic-tac-toe_old.rb | 139 +----------------- 3 files changed, 8 insertions(+), 138 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 14a67ce..6df832a 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -6,6 +6,7 @@ class TicTacToe def initialize(first_player=nil, player_symbol=nil) @current_player = nil @players = [:player, :computer] + @player = "Anonymous" @board = { A1: " ", A2: " ", A3: " ", B1: " ", B2: " ", B3: " ", @@ -83,6 +84,7 @@ def validate_move?(move) false end def next_player + @current_player = (PLAYERS - [@current_player])[0] end def current_state output = '' @@ -104,6 +106,7 @@ def current_state output << " | " end output + # @board end def determine_winner victory = false diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index f64b949..9543db8 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -4,10 +4,14 @@ puts @game.welcome_player until @game.over? + puts "Player is #{@game.player}" + puts @game.current_player case @game.current_player when "Computer" + puts "Computer is moving" @game.computer_move when @game.player + puts "player is moving" @game.indicate_player_turn @game.player_move end diff --git a/week7/homework/tic-tac-toe_old.rb b/week7/homework/tic-tac-toe_old.rb index 8e54cf9..4bcfe98 100644 --- a/week7/homework/tic-tac-toe_old.rb +++ b/week7/homework/tic-tac-toe_old.rb @@ -1,138 +1 @@ -# re-do this... a.rotate! to keep track of who is who? a.rotate![0] - - -class TicTacToe - attr_accessor :player, :board, :over - attr_reader :player_symbol, :player_entry, :player_move, :computer_symbol, :over - SYMBOLS = [:X, :O] - PLAYERS = [@player, "Computer"] - def initialize(first_player=nil,player_symbol=nil) - @board = { - A1: nil, A2: nil, A3: nil, - B1: nil, B2: nil, B3: nil, - C1: nil, C2: nil, C3: nil - } - if first_player.nil? - random_start - else - @current_player = first_player - end - if player_symbol.nil? - random_symbol - else - # set_symbol(@current_player,first_player_symbol) - @player_symbol = player_symbol - @computer_symbol = (SYMBOLS - [@player_symbol]).first - end - @player = "Anonymous" - end - def player=(player) - @player = player - end - def random_symbol - if rand(0..1).zero? - @player_symbol = SYMBOLS[0] - @computer_symbol = SYMBOLS[1] - else - @player_symbol = SYMBOLS[1] - @computer_symbol = SYMBOLS[0] - end - end - def random_start - if rand(0..1).zero? - @current_player = :player - else - @current_player = :computer - end - end - def welcome_player - "Welcome #{@player}" - end - - def over? - true if draw? || player_won != nil - false - end - def current_player - if @current_player == :computer - "Computer" - else - @player - end - end - def next_turn - #@current_player = (["Computer",@player]-[@current_player])[0] - @current_player = (PLAYERS - [@current_player])[0] - end - def set_first_player(p) - @current_player = p - end - def set_symbol(p,s) - #set_ionstance_method("@#{p}") - end - def indicate_player_turn - if current_player == :computer - puts "Computer's Move:" - else - puts "#{player}'s Move:" - end - end - def get_player_move - @player_entry = gets.chomp.to_sym - end - def player_move - get_player_move - @board[@player_entry] = @player_symbol - next_turn - @player_entry.to_sym - end - def open_spots - @board.select { |k,v| v.nil?} - end - def computer_move - computer_square = open_spots.keys.sample - @board[computer_square.to_sym] = @computer_symbol - #computer_square - next_turn - computer_square - end - def current_state - output = '' - @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output << "\n _ _ _ \n" - @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| - output << v.to_s unless v.nil? - output << " " if v.nil? - output << " | " - end - output - end - def determine_winner - @over = 1 - end - def validate_move? - # Check to see if a player move is valid... - end - def spots_open? - if @board.select {|k,v| v.nil?}.empty? - false - else - true - end - end - def draw? - true if spots_open? == false - end - def player_won - end -end +d From 683355386f4eb38e99fed735de0558f4f6d85e6a Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Mon, 3 Dec 2012 20:46:25 -0800 Subject: [PATCH 25/27] almost there again --- week7/homework/play_game.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 9543db8..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -4,14 +4,10 @@ puts @game.welcome_player until @game.over? - puts "Player is #{@game.player}" - puts @game.current_player case @game.current_player when "Computer" - puts "Computer is moving" @game.computer_move when @game.player - puts "player is moving" @game.indicate_player_turn @game.player_move end From f600408d24c45ae02a67bbe5023d98929bc48856 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Tue, 11 Dec 2012 16:16:17 -0800 Subject: [PATCH 26/27] Fix bug --- week7/homework/features/step_definitions/tic-tac-toe.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 6df832a..5c8d810 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -60,6 +60,7 @@ def open_spots end def computer_move move = open_spots.keys.sample + next_player(:player) process_move(:computer, move) end def player_move @@ -67,6 +68,7 @@ def player_move until validate_move?(move.to_sym) move = get_player_move.to_sym end + next_player(:computer) process_move(:player,move) end @@ -75,7 +77,6 @@ def process_move(player, move) return 1 unless validate_move?(move) @board[move] = @computer_symbol if player == :computer @board[move] = @player_symbol if player == :player - next_player move end # This checks to see if the move falls upon an open spot @@ -83,8 +84,9 @@ def validate_move?(move) return true if open_spots.include?(move) false end - def next_player - @current_player = (PLAYERS - [@current_player])[0] + def next_player(player) + @current_player = player + # @current_player = (PLAYERS - [@current_player])[0] end def current_state output = '' @@ -113,6 +115,7 @@ def determine_winner @player_won = false @computer_won = false if @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != " " + victory = true winner_symbol = @board[:A1] elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != " " victory = true From c81f8bbf5dc18bcf3afb3d627fbd11923b136fc6 Mon Sep 17 00:00:00 2001 From: Nikky Southerland Date: Sun, 29 Jun 2014 18:25:55 -0700 Subject: [PATCH 27/27] Add new --- week10/class_materials/hi.rb | 9 ++ week10/class_materials/sinatra.rb | 153 ++++++++++++++++++++++++++++++ week10/class_materials/struct.rb | 11 +++ week10/index.html | 1 + week10/index.html.1 | 1 + 5 files changed, 175 insertions(+) create mode 100644 week10/class_materials/sinatra.rb create mode 100644 week10/class_materials/struct.rb create mode 100644 week10/index.html create mode 100644 week10/index.html.1 diff --git a/week10/class_materials/hi.rb b/week10/class_materials/hi.rb index 0a722c9..39218a7 100644 --- a/week10/class_materials/hi.rb +++ b/week10/class_materials/hi.rb @@ -9,3 +9,12 @@ "Renée is the coolest teacher ever!"+ "
Click Here" end + +get '/yousuck' do + stream do |out| + out << "fail\n" + sleep 0.5 + out << "\nsauce" + sleep 1 + end +end \ No newline at end of file diff --git a/week10/class_materials/sinatra.rb b/week10/class_materials/sinatra.rb new file mode 100644 index 0000000..f7a9b48 --- /dev/null +++ b/week10/class_materials/sinatra.rb @@ -0,0 +1,153 @@ +require 'sinatra' + +get '/' do + "

NIKKY IS AWESOME YAY WHAT A CHAMP

" + "Find out why" +end + +get '/coolest' do + "Because he is, fool" +end + +get '/yousuck' do + stream do |out| + out << "fail\n" + sleep 0.5 + out << "\nsauce" + sleep 1 + end + +end + +get '/pledge' do +%Q{ + + + +KUOW Pledge Drive + + + +
+-------------------------------------------------------------------------------+
+|                __  ___  __    __    ______   ____    __    ____               |
+|               |  |/  / |  |  |  |  /  __  \  \   \  /  \  /   /               |
+|               |  '  /  |  |  |  | |  |  |  |  \   \/    \/   /                |
+|               |    <   |  |  |  | |  |  |  |   \            /                 |
+|               |  .  \  |  `--'  | |  `--'  |    \    /\    /                  |
+|               |__|\__\  \______/   \______/      \__/  \__/                   |
+|            .______    __       _______  _______   _______  _______            |
+|            |   _  \  |  |     |   ____||       \ /  _____||   ____|           |
+|            |  |_)  | |  |     |  |__   |  .--.  |  |  __  |  |__              |
+|            |   ___/  |  |     |   __|  |  |  |  |  | |_ | |   __|             |
+|            |  |      |  `----.|  |____ |  '--'  |  |__| | |  |____            |
+|            | _|      |_______||_______||_______/ \______| |_______|           | 
+|                _______  .______       __  ____    ____  _______               |
+|               |       \ |   _  \     |  | \   \  /   / |   ____|              |
+|               |  .--.  ||  |_)  |    |  |  \   \/   /  |  |__                 |
+|               |  |  |  ||      /     |  |   \      /   |   __|                |
+|               |  '--'  ||  |\  \----.|  |    \    /    |  |____               |
+|               |_______/ | _| `._____||__|     \__/     |_______|              |
+|                                                                               |
+|                          Strength Through Unity                               |
+|                        Unity Through Public Radio                             |
++-------------------------------------------------------------------------------+
+
+
+ +} +end \ No newline at end of file diff --git a/week10/class_materials/struct.rb b/week10/class_materials/struct.rb new file mode 100644 index 0000000..8a8986a --- /dev/null +++ b/week10/class_materials/struct.rb @@ -0,0 +1,11 @@ +Struct.new('Person', :full_name, :email) do + def name + "hello my name is #{:full_name}" + end +end +p = Struct::Person.new('Renee', 'renee@awesome.us') + +# or + +Person = Struct.new(:full_name, :email) +p = Person.new('renee', 'nikky@awesome.us') \ No newline at end of file diff --git a/week10/index.html b/week10/index.html new file mode 100644 index 0000000..6a79207 --- /dev/null +++ b/week10/index.html @@ -0,0 +1 @@ +
Find out why \ No newline at end of file diff --git a/week10/index.html.1 b/week10/index.html.1 new file mode 100644 index 0000000..6a79207 --- /dev/null +++ b/week10/index.html.1 @@ -0,0 +1 @@ +Find out why \ No newline at end of file