diff --git a/mid_term/wish_list_spec.rb b/mid_term/wish_list_spec.rb deleted file mode 100644 index 37beab5..0000000 --- a/mid_term/wish_list_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "#{File.dirname(__FILE__)}/wish_list" - -describe WishList do - before :each do - @wish_list = WishList.new - @wish_list.wishes = ["Lamborghini", "Corn Starch and Water Moat", "Vegan Bacon Ice Cream", "Rubber Chicken", "Free Tickets to Skyfall"] - end - - it "should mixin Enumerable" do - @wish_list.is_a?(Enumerable).should be_true - end - - context "#each" do - it "should give me a numberd list" do - @wish_list.map{|w| w}.should eq ["1. Lamborghini", "2. Corn Starch and Water Moat", "3. Vegan Bacon Ice Cream", "4. Rubber Chicken", "5. Free Tickets to Skyfall"] - end - end -end \ No newline at end of file diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb deleted file mode 100644 index f4c2f0b..0000000 --- a/week1/exercises/rspec_spec.rb +++ /dev/null @@ -1,95 +0,0 @@ -# encoding: utf-8 - -describe "The Rspec ruby gem" do - - context "Domain Specific Language" do - - it "creates examples with the #it keyword" do - - # this test code passes, so this example passes - 1.should eq 1 - - end - - it "has keywords like #context, and #describe to help organize the spec, or specification" do - - # test code goes here - (1+2).should eq 3 - - end - - it "has easily readable methods like #should to test any object" do - - "Hello".should eq "Hello" - - end - - it "has #should_not to test for negative cases" do - - 1.should_not eq 2 - - end - - it "creates readable predicate methods" do - - # Integers have #zero? and #nil? predicate methods, so - # rspec automatically supports the #be_zero and #be_nil parameter to #should_not method - 1.should_not be_zero - 1.should_not be_nil - - end - - it "alerts you when examples fail" do - - # When this example fails, - # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 - - end - - it "supports placeholder examples that lack code (like this one)" - - it "requires that examples use expectations (like #should) to work properly" do - - # The following expression is false. - # However, this example PASSES because no expectation was created. - true == false - - # The following line of code is correct, and would cause the example to fail: - # true.should == false - - # Lesson: It's easy to write bad tests. - - end - - it "should count the characters in my name" do - "Renée".should have(5).characters - end - - it "should check how to spell my name" do - "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 -12 - end - it "should count the charaters in your name" do - "Nell".should have(4).characters - end - - it "should check basic math" do - (1+2+3+4+5).should eq 15 - end - - it "should check basic spelling" do - "Field".should include('ie') - end - end - -end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 0adfd69..e40b790 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,8 +1,28 @@ -Please read: -Chapter 3 Classes, Objects, and Variables -p.90-94 Strings - 1. What is an object? +<<<<<<< HEAD + + Object is the root of Ruby's class hierarchy. Its methods are available to all classes unless explicitly overridden. + +2. What is a variable? + + A variable is a reference to an object. + +3. What is the difference between an object and a class? + + All objects are instances of a class. + +4. What is a String? + + A string is a sequence of characters. Strings are objects of class String. + +5. What are three messages that I can send to a string object? Hint: think methods + + downcase, upcase, strip + +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + + Two ways to define a String literal are single quoted string and double quoted string. A double quoted string can undergo additional subsitutions such as Tab, Space, Return, Escape or Backspace. +======= An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. 2. What is a variable? @@ -22,3 +42,4 @@ split - returns an array of strings made up of the original string separated on 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same. +>>>>>>> ea9aa93fcf3644ee7d901c086bc1b1ebc77f6f44 diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 2f188f6..2667b39 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +#encoding: utf-8 # Please make these examples all pass # You will need to change the 3 pending tests @@ -13,6 +13,16 @@ @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 +<<<<<<< HEAD + @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")) +======= @my_string.should have(@my_string.size).characters end it "should be able to split on the . charater" do @@ -21,6 +31,7 @@ end it "should be able to give the encoding of the string" do @my_string.encoding.should eq (Encoding.find("UTF-8")) +>>>>>>> ea9aa93fcf3644ee7d901c086bc1b1ebc77f6f44 end end end diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 50bc054..64fc19e 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,5 +1,16 @@ class Book +<<<<<<< HEAD + attr_accessor :title, :page_count + + def initialize(title, page_count) + @title = title + @page_count = page_count + end + + def page_count + return "Page count is #{@page_count}" +======= attr_accessor :title, :pages def initialize(title, pages) @@ -9,5 +20,6 @@ def initialize(title, pages) def page_count "Page count is #{@pages}" +>>>>>>> 476e4b543ee68aad8bb809afdfe2207afd39e8e5 end end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index bb22819..bdb8842 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,4 +1,8 @@ require './book.rb' +<<<<<<< HEAD +require './spec_helper.rb' +======= +>>>>>>> 476e4b543ee68aad8bb809afdfe2207afd39e8e5 describe Book do before :each do diff --git a/week2/exercises/lor b/week2/exercises/lor new file mode 100644 index 0000000..e69de29 diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 3af5583..c54dfa4 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -4,7 +4,13 @@ adjective = gets.chomp puts "Please enter a past tense action verb" verb_past_tense = gets.chomp +<<<<<<< HEAD +puts "Enter another noun" +noun1 = gets.chomp +story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and ate the #{noun1}" +======= puts "What does the #{noun} say?" says = gets.chomp story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" +>>>>>>> 476e4b543ee68aad8bb809afdfe2207afd39e8e5 puts story diff --git a/week2/exercises/spec_helper.rb b/week2/exercises/spec_helper.rb new file mode 100644 index 0000000..53828f6 --- /dev/null +++ b/week2/exercises/spec_helper.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.color_enabled = true +end \ No newline at end of file diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..903473a 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,6 +3,26 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +<<<<<<< HEAD + + The difference between a Hash and an Array is a Hash can be indexed with an object of any type, such as a symbol or strig whereas an array is indexed with a non-negative integer. + +2. When would you use an Array over a Hash and vice versa? + + An array is useful for storing a list of items where an index isn't necessary. A hash would be used to store a collection of items with a descriptive index (key) for easier look-up later. + +3. What is a module? Enumerable is a built in Ruby module, what is it? + + A module groups together methods, classes and constants. Enumerable is a collecton of classes with traversal and searching methods with the ability to sort. + +4. Can you inherit more than one thing in Ruby? How could you get around this problem? + + Ruby is a single-inheritance language. Although you can use "mixins" and include a module within a class definition to access to the module's instance methods. + +5. What is the difference between a Module and a Class? + + The difference between a Module and a Class is that a module cannot be instantiated. A class may inherit from another class. A module cannot inherit from anything. +======= An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order. 2. When would you use an Array over a Hash and vice versa? @@ -16,3 +36,4 @@ No, multiple inheritance is not allowed in Ruby. You can include multiple module 5. What is the difference between a Module and a Class? A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. +>>>>>>> 529b28228eac9e841374d40584eb6341ab03b2dd diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index fa35ccd..7a0fe7f 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,4 +1,40 @@ module SimonSays +<<<<<<< HEAD + + def echo(word) + @word = word + return "#{@word}" + end + + def shout(word) + @word = word.upcase + return "#{@word}" + end + + def repeat(word, times=2) + + @word = "#{word}" + @final_word = @word + + for i in 1...times + @final_word = @final_word + " " + "#{word}" + end + + return @final_word + end + + def start_of_word(word, end_pnt) + @word = word + return @word[0...end_pnt] + end + + def first_word(word) + @word = word.scan(/\w+/) + return @word[0] + + end +end +======= def echo(st) st end @@ -20,3 +56,4 @@ def repeat(st, t=2) ([st]*t).join(' ') end end +>>>>>>> 529b28228eac9e841374d40584eb6341ab03b2dd diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index f8916e3..482c54c 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,4 +1,23 @@ class Calculator +<<<<<<< HEAD + + def sum(num) + num.inject(0,:+) + end + + def multiply(num) + num.inject(:*) + end + + def power(num) + num.inject(:**) + end + + def factorial(num) + (1..num).inject(0,:*) + end +end +======= def sum(array) array.inject(0){|sum, x| sum +x} end @@ -22,3 +41,4 @@ def pow_fac(base=nil, p) (1..p).to_a.inject(1){|f,v| f *= base || v} end end +>>>>>>> f7e675fa7a88f9cdfc4b342f33e2567d897b5075 diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..9ce220d 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,3 +1,4 @@ +require "./spec_helper.rb" require "#{File.dirname(__FILE__)}/calculator" describe Calculator do @@ -26,6 +27,43 @@ # Once the above tests pass, # write tests and code for the following: +<<<<<<< HEAD +describe "#multiply" do + it "multiplies two numbers" do + @calculator.multiply([5,6]).should == 30 + end + + it "multiplies an array of numbers" do + @calculator.multiply([2,4,2,4]).should == 64 + end + end + describe "#power" do + it "raises one number to the power of another number" do + @calculator.power([2,4]) == 16 + end +end + + # http://en.wikipedia.org/wiki/Factorial + describe "#factorial" do + it "computes the factorial of 0" do + @calculator.factorial(0) == 0 + end + + it "computes the factorial of 1" do + @calculator.factorial(1) == 1 + end + + it "computes the factorial of 2" do + @calculator.factorial(2) == 2 + end + + it "computes the factorial of 5" do + @calculator.factorial(5) == 120 + end + it "computes the factorial of 10" do + @calculator.factorial(10) == 3628800 + end +======= describe "#multiply" do it "multiplies two numbers" do @calculator.multiply(2,2).should eq 4 @@ -63,6 +101,7 @@ @calculator.fac(10).should eq 3628800 end +>>>>>>> f7e675fa7a88f9cdfc4b342f33e2567d897b5075 end end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index 08067b8..427c6a4 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,6 +5,28 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +<<<<<<< HEAD + + A symbol is a string that uses the same space in memory. A symbol is also a constant name that does not have to be predeclared and are guaranteed to be unique. + +2. What is the difference between a symbol and a string? + + A symbol cannot change, but a string can change. + +3. What is a block and how do I call a block? + + A block is a piece of code that can be associated to method invocation. The block is the code between two curly braces or a do and an end. + +4. How do I pass a block to a method? What is the method signature? + + A block can be passed to a method by putting the block at the end of the source line containing the method call. Methods with parameters appear before the block. + + The method signature is a combination of the method name and its parameters. + +5. Where would you use regular expressions? + + Regular expressions can be used for matching strings with patterns, changing strings with patterns, negative matching, repetition of chaacters or patterns, grouping, subsitution, and replacement to name a few. +======= A symbol is a static name or identifier. 2. What is the difference between a symbol and a string? @@ -26,3 +48,4 @@ end 5. Where would you use regular expressions? Regular expressions are used for pattern matching and replacement with strings. An example would be if I wanted to write a syntax checker for some text that checked if each sentance ended with a period, started with a space and then a capital letter. +>>>>>>> f7e675fa7a88f9cdfc4b342f33e2567d897b5075 diff --git a/week3/homework/spec_helper.rb b/week3/homework/spec_helper.rb new file mode 100644 index 0000000..45878fd --- /dev/null +++ b/week3/homework/spec_helper.rb @@ -0,0 +1,10 @@ +RSpec.configure do |config| + # Use color in STDOUT + config.color_enabled = true + + # Use color not only in STDOUT but also in pagers and files + config.tty = true + + # Use the specified formatter + config.formatter = :documentation # :progress, :html, :textmate +end \ No newline at end of file diff --git a/week4/class_materials/odd_number.rb b/week4/class_materials/odd_number.rb index d41cf85..3eec650 100644 --- a/week4/class_materials/odd_number.rb +++ b/week4/class_materials/odd_number.rb @@ -1,9 +1,15 @@ class OddNumber attr_accessor :value +<<<<<<< HEAD + def initialize (value) + @value = value + end +======= def initialize(value) @value = value end +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 def succ new_val = nil @@ -16,7 +22,13 @@ def succ end def <=> (other) +<<<<<<< HEAD + end + +end +======= @value <=> other.value end end +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb index 67b7681..5052665 100644 --- a/week4/class_materials/timer.rb +++ b/week4/class_materials/timer.rb @@ -1,8 +1,21 @@ class Timer +<<<<<<< HEAD + +======= +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 def self.time_code(n=1) start_time = Time.now n.times{yield} end_time = Time.now +<<<<<<< HEAD + + end_time - start_time + end + + +end +======= (end_time - start_time) / n.to_f end -end \ No newline at end of file +end +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb index f71414a..6bddaab 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -17,16 +17,29 @@ flag.should be_true end +<<<<<<< HEAD + it "should run out code multiple times" do + counter = 0 + result = Timer.time_code(17){counter += 1} +======= it "should run our code multiple times" do counter = 0 result = Timer.time_code(17) {counter += 1} +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 counter.should equal 17 end it "should give the average time" do +<<<<<<< HEAD + Time.stub(:now).and_return(0,1) + result = Timer.time_code(1) {} + result.should be_within(0.1).of(1.0) + end +======= Time.stub(:now).and_return(0,10) result = Timer.time_code(10) { } result.should be_within(0.1).of(1) end +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 end diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb index fad4e2f..18a05ce 100644 --- a/week4/exercises/worker.rb +++ b/week4/exercises/worker.rb @@ -1,5 +1,20 @@ class Worker +<<<<<<< HEAD + + def self.work(n=1) + result = nil + + n.times do + result = yield + end + + result + + end +end +======= def self.work(n=1) n.times.inject(nil){yield} end -end \ No newline at end of file +end +>>>>>>> f86ef48f01b2f530487a4fe320074634b7040078 diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..518499c 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,25 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + + Ruby reads files by using the IO Class. + 2. How would you output "Hello World!" to a file called my_output.txt? + + To output "Hello World" to a file called my_output.txt you would use the following: + + File.open("my_output.txt", "w") do |file| + file.puts "Hello World!" + end + 3. What is the Directory class and what is it used for? + + The Directory class is a way to list directories and it's contents. This class provides a way to intereact with directories, such as create new ones or delete existing directories. + 4. What is an IO object? + + An IO object is a bidirectional channel between a Ruby Program and some external resource. + 5. What is rake and what is it used for? What is a rake task? + + Rake is a ruby specific gem of the makefiles. Rake can be used to set up prerequisite tasks. This is especially useful for deployments. One rake task in particular can run a unit test for Ruby. \ No newline at end of file diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index ce332af..4887d56 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -2,9 +2,31 @@ task :test do puts "Hello World!" end +<<<<<<< HEAD +desc "Read names files" +task :create_names do +store_names = [] + File.open("names", "w") do |names| + open_file_use_lines("names") do |l| + name = l + store_names << name + puts store_names + end + end +end + +def open_file_use_lines(file="names") + File.open(file) do |f| + f.each do |l| + yield "#{l.chomp}.git" + end + end +end +======= task :make_class_dir do Dir.mkdir("class") end task :output +>>>>>>> 8e40b4405eff23831e44e86f2b0d147dd1fd77c9 diff --git a/week5/exercises/names b/week5/exercises/names index 9eec0ec..e69de29 100644 --- a/week5/exercises/names +++ b/week5/exercises/names @@ -1,35 +0,0 @@ -Ben -BrianT -BrianWard -BryanWilliams -Cheri -Chris B -ChristopherF -ChristopherT -Danny -Dimitry -Eddie -Emily -Gregory -Josh -Karen -Kat -Kelli -Kris -Mallory -Nell -NicholasP -Nicole -Nikky -Peter -Price -Renée -Ryan -Santy -Serene -Sol -Steven -Strand -Timothy -Will -Yan diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb index 4cb1264..7deddf1 100644 --- a/week7/exercises/features/step_definitions/converter.rb +++ b/week7/exercises/features/step_definitions/converter.rb @@ -1,5 +1,10 @@ class Converter +<<<<<<< HEAD + + def initialize (value) +======= def initialize(value) +>>>>>>> 7a51db287e3acee6bc9678f2d9531ecd3e8bc7c4 @value = value.to_f end @@ -12,6 +17,13 @@ def convert end def Fahrenheit_convertion +<<<<<<< HEAD + ((@value - 32) / 1.8).round(1) + end + +end +======= (((@value - 32.0) /5.0) * 9.0).round(1) end -end \ No newline at end of file +end +>>>>>>> 7a51db287e3acee6bc9678f2d9531ecd3e8bc7c4 diff --git a/week7/exercises/features/step_definitions/converter_steps.rb b/week7/exercises/features/step_definitions/converter_steps.rb index 5a12ae5..8ed53f3 100644 --- a/week7/exercises/features/step_definitions/converter_steps.rb +++ b/week7/exercises/features/step_definitions/converter_steps.rb @@ -1,4 +1,19 @@ Given /^I have entered (\d+) into the converter$/ do |arg1| +<<<<<<< HEAD + @converter = Converter.new(arg1) +end + +Given /^I set the type to Fahrenheit$/ do + @converter.type = "Fahrenheit" +end + +When /^I press convert$/ do + @result = @converter.convert +end + +Then /^the result returned should be (\d+)\.(\d+)$/ do |arg1, arg2| + @result.should eq "#{arg1}.#{arg2}".to_f +======= @converter = Converter.new(arg1) end @@ -12,4 +27,5 @@ Then /^the result returned should be (\d+)\.(\d+)$/ do |arg1, arg2| @result.should == "#{arg1}.#{arg2}".to_f +>>>>>>> 7a51db287e3acee6bc9678f2d9531ecd3e8bc7c4 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..a5e8a51 --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy.rb @@ -0,0 +1,16 @@ +class Puppy + + attr_accessor :name + + def pet + "The puppy wags its tail!" + end + + def ring_bell + "it wags its tail!" + end + + def go_potty + "it will not pee on the floor!" + end +end 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..b17ad80 --- /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 + @pet = @puppy.pet +end + +Then /^the puppy wags its tail$/ do + @pet.should eq "The puppy wags its tail!" +end + +Given /^its name is Bella$/ do + @puppy.name = "Bella" +end + +When /^we ring the bell$/ do + @ring = @puppy.ring_bell +end + +When /^it wags its tail$/ do + @ring.should eq "it wags its tail!" +end + +Then /^we must take it out$/ do + @out = @puppy.go_potty +end + +Then /^then it will not pee on the floor$/ do + @out.should eq "it will not pee on the floor!" +end \ No newline at end of file diff --git a/week7/homework/features/pirate.rb b/week7/homework/features/pirate.rb new file mode 100644 index 0000000..af32b07 --- /dev/null +++ b/week7/homework/features/pirate.rb @@ -0,0 +1,11 @@ +class Pirate + + def translate(value) + if value == "Hello Friend" + @value = "Ahoy Matey" + else + @value = "Shiber Me Timbers You Scurvey Dogs!!" + end + @value + end +end \ No newline at end of file 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..4b68cb4 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -0,0 +1,20 @@ +Gangway /^I have a PirateTranslator$/ do + @pirateTranslator = Pirate.new() +end + +Blimey /^I say 'Hello Friend'$/ do + "Hello Friend" +end + +Blimey /^I hit translate$/ do + @translate = @pirateTranslator.translate("Hello Friend") + @translate1 = @pirateTranslator.translate("") +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @translate.should eq "Ahoy Matey" +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @translate1.should eq "Shiber Me Timbers You Scurvey Dogs!!" +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..b36fc9b 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| @@ -43,7 +43,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -77,11 +77,11 @@ @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +Then /^it is now the computers turn$/ do @game.current_player.should eq "Computer" end -When /^there are three X's in a row$/ do +When /^there are three Xs in a row$/ do @game = TicTacToe.new(:computer, :X) @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X end @@ -122,3 +122,15 @@ @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) @game.player_move.should eq arg1.to_sym end + +Given /^it is the computers turn$/ do + @game = TicTacToe.new(:computer, :O) +end + +When /^there are three Xs in a row$/ do + @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X +end + +Then /^it is now the computers turn$/ do + @game.current_player.should eq "Computer" +end \ No newline at end of file diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..71a5970 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -1,7 +1,7 @@ Feature: Tic-Tac-Toe Game As a game player I like tic-tac-toe In order to up my skills - I would like to play agaist the computer + I would like to play against the computer Scenario: Begin Game Given I start a new Tic-Tac-Toe game diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb new file mode 100644 index 0000000..70b0640 --- /dev/null +++ b/week7/homework/features/tic-tac-toe.rb @@ -0,0 +1,152 @@ +class TicTacToe + + attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board + SYMBOLS = [:X,:O] + + def initialize(starter=nil, symbol=nil) + @starter = starter + @symbol = symbol + + unless @symbol.nil? + if @starter == "Computer" + @computer_symbol = @symbol + @player_symbol = :O + @computer = "Computer" + else + @player_symbol = @symbol + @computer_symbol = :O + @player = @starter + end + end + + #current_player + + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + if @symbol.nil? + @player_symbol = :X + @computer_symbol = :O + @current_player = [@player, "Computer"].sample + else + @current_player = @starter + if @player_symbol == :X then @computer_symbol = :Y else @computer_symbol = :X end + if @computer_symbol == :X then @player_symbol = :Y else @player_symbol = :X end + end + @current_player + end + + def get_player_move + #@current_player = (@current_player == SYMBOL::X) ? SYMBOL::O : SYMBOL::X + @get_player_move = gets.chomp + end + + def indicate_player_turn + puts "#{@current_player}'s Move:" + @indicate_player_turn = @current_player + end + + def indicate_player_move + @indicate_player_move + end + + def computer_move + @computer_move =@open_spots.sample + + end + + def player_move + board[get_player_move] = @player_symbol + @player_move = get_player_move.to_sym + end + + def current_state + @current_state = @board.values.to_s + end + + def determine_winner + i = 0 + winning_combos = [] + @winning_combos = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + [:A1,:B2,:C3], + [:C1,:B2,:A3] + ] + + while i < 8 do + unless winning_combos[i].unique.length != 1 + if winning_combos[i].includes "X" && player_symbol = "X" + player_won? + elsif winning_combos[i].includes "Y" && player_symbol = "Y" + player_won? + elsif winning_combos[i].includes "X" && computer_symbol = "X" + computer_won? + elsif winning_combos[i].includes "Y" && computer_symbol = "Y" + computer_won? + else + end + end + + if spots_open? == false + draw? + else + if @current_player == "Computer" + @current_player = @player + else + current_player = "Computer" + end + end + end + end + + def open_spots + @open_spots = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} + end + + def spots_open? + if @board.values.include?(" ") then true else false end + end + + def draw? + + @draw = false + + unless @spots_open == false + @draw = true + end + end + + def player_won? + @player_won == true + end + + def computer_won? + @computer_won == true + end + + def over? + if @computer_won == true + @over = true + elsif @player_won == true + @over = true + elsif @draw == true + @over = true + else + @over = false + 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 diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..2bb2e3e 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,21 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + + The method_missing method in Object with a default that throws and error and terminates the program. If a method is non-existent in self then ruby will look for the method in the superclass chain. If the method is not found it will call method_missing. The method_missing can be used to simulate the existence of methods in the receiver. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + + The Eigenclass is an object's own class. The eiganclass becomes the class of the object and is pushed up in the lookup chain as the superclass. Singleton methods are defined on the object itself and not the class. + 3. When would you use DuckTypeing? How would you use it to improve your code? + + DuckTyping could be used for testing as well as applications. DuckTyping help create flexible code. With DuckTyping, you don't need to test the type of arguments, which makes the code more flexible. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + + The difference between a class method and and instance method is class methods are called on a class whereas instance methods are called on an instance of a class. The difference between instance_eval and class_eval is instance_eval evaluates code against a single object instance (within singleton class) whereas class_eval acts as if you are in the body of a class definition, method definitions will define instance methods. + 5. What is the difference between a singleton class and a singleton method? + + The difference between a singleton class and a singleton method is a singleton method is an instance method associated with one specific object whereas a singleton class is an anonymous class created by subclassing the class associated with a particular object. \ No newline at end of file diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..baa7327 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -4,17 +4,15 @@ def initialize(pillows, cushions) @cushions = cushions end - def pillow_colors - @pillows.join(", ") - end - - def cushion_colors - @cushions.join(", ") - end - [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end end + + [:pillows, :cushions].each do |s| + define_method("colors_#{s}") do + instance_variable_get("@#{s}").join(",") + end + end end