diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e69de29 diff --git a/gem_project_rubywinter2014 b/gem_project_rubywinter2014 new file mode 160000 index 0000000..f977919 --- /dev/null +++ b/gem_project_rubywinter2014 @@ -0,0 +1 @@ +Subproject commit f9779198f8ac3d626bc0e5ff9b15e4445bee7e16 diff --git a/gem_test/gem_test.rb b/gem_test/gem_test.rb new file mode 100644 index 0000000..48b3208 --- /dev/null +++ b/gem_test/gem_test.rb @@ -0,0 +1,4 @@ +require 'open_exchange_rates_conversion' + +new_currency = Converter.new + puts new_currency.currency_exchange('USD', 1, '912735b01e8143bf8dee944d7673ed06') diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt deleted file mode 100644 index e38c84d..0000000 --- a/midterm/instructions_and_questions.txt +++ /dev/null @@ -1,39 +0,0 @@ -Instructions for Mid-Term submission and Git Review (10pts): - - Create a git repository for your answers - - Add and Commit as you work through the questions and programming problems - - Your git log should reflect your work, don't just commit after you have finished working - - Use .gitignore to ignore any files that are not relevant to the midterm - - E-mail me your ssh public key - - I will email you back with your repository name - - Add a remote to your git repository: git@reneedv.com:RubyWinter2014/YOURREPOSITORYNAME.git - - Push your changes to the remote - - After 6pm Thursday February 20th you will not be able to push to your remote repository (or clone). - - Questions (20pts): - - What are the three uses of the curly brackets {} in Ruby? - - What is a regular expression and what is a common use for them? - - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? - - Are these two statements equivalent? Why or Why Not? - 1. x, y = "hello", "hello" - 2. x = y = "hello" -- What is the difference between a Range and an Array? -- Why would I use a Hash instead of an Array? -- What is your favorite thing about Ruby so far? -- What is your least favorite thing about Ruby so far? - - Programming Problems (10pts each): - - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. - - The EvenNumber class should: - - Only allow even numbers - - Get the next even number - - Compare even numbers - - Generate a range of even numbers -- Make the rspec tests in wish_list_spec.rb pass by writing a WishList class - - The WishList class should: - - Mixin Enumerable - - Define each so it returns wishes as strings with their index as part of the string - -Mid-Term Spec (50pts): -- Make the tests pass. - - diff --git a/midterm/mid_term_spec.rb b/midterm/mid_term_spec.rb deleted file mode 100644 index 0556a97..0000000 --- a/midterm/mid_term_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -require "#{File.dirname(__FILE__)}/turkey" - -describe Turkey do - - before do - @turkey = Turkey.new(10) - end - - it "should report the turkey weight" do - @turkey.weight.should equal 10 - end - - it "should be a kind of animal" do - @turkey.kind_of?(Animal).should be_true - end - - it "should gobble speak" do - @turkey.gobble_speak("Hello I Am a Turkey. Please Don't Eat Me.").should eq "Gobble Gobble Gobble gobble Gobble. Gobble Gobb'le Gobble Gobble." - end - -end - -require "#{File.dirname(__FILE__)}/thanksgiving_dinner" - -describe ThanksgivingDinner do - - before do - @t_dinner = ThanksgivingDinner.new(:vegan) - @t_dinner.guests = ["Aunt Petunia", "Uncle Vernon", "Aunt Marge", "Dudley", "Harry"] # I know I just made a British family celebrate Thanksgiving, but it could be worse: It could have been the 4th of July! :) - end - - it "should be a kind of dinner" do - @t_dinner.kind_of?(Dinner).should be_true - end - - # Use inject here - it "should sum the letters in each guest name for the seating chart size" do - @t_dinner.seating_chart_size.should eq 45 - end - - it "should provide a menu" do - @t_dinner.respond_to?(:menu).should be_true - end - - context "#menu" do - - it "should have a diet specified" do - @t_dinner.menu[:diet].should eq :vegan - end - - it "should have proteins" do - @t_dinner.menu[:proteins].should eq ["Tofurkey", "Hummus"] - end - - it "should have vegetables" do - @t_dinner.menu[:veggies].should eq [:ginger_carrots , :potatoes, :yams] - end - - # Dinners don't always have dessert, but ThanksgivingDinners always do! - it "should have desserts" do - @t_dinner.menu[:desserts].should eq({:pies => [:pumkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}) - end - - end - - # Use String interpolation, collection methods, and string methods for these two examples - it "should return what is on the dinner menu" do - @t_dinner.whats_for_dinner.should eq "Tonight we have proteins Tofurkey and Hummus, and veggies Ginger Carrots, Potatoes, and Yams." - end - - it "should return what is on the dessert menu" do - @t_dinner.whats_for_dessert.should eq "Tonight we have 5 delicious desserts: Pumkin Pie, Chocolate Moose, and 3 molds: Cranberry and Mango and Cherry." - end -end diff --git a/midterm/wish_list_spec.rb b/midterm/wish_list_spec.rb deleted file mode 100644 index 9ba4bce..0000000 --- a/midterm/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 Spamalot"] - 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 Spamalot"] - end - end -end \ No newline at end of file diff --git a/week1/.DS_Store b/week1/.DS_Store new file mode 100644 index 0000000..29a0684 Binary files /dev/null and b/week1/.DS_Store differ diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1152c22..c18b495 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -2,7 +2,7 @@ describe "The Rspec ruby gem" do - context "Domain Specific Language" do + context "Domain Specific Language" do it "creates examples with the #it keyword" do @@ -63,34 +63,33 @@ 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 -6 + it "should know order of operations" do + # Fix the Failing Test + # Order of Operations is Please Excuse My Dear Aunt Sally: + # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction + ((((1+2)-5)*6)/2).should eq -6 + end + it "should count the characters in your name" do + John.should have(4).characters end - it "should count the characters in your name" do - "Tom".should have(3).characters - end - - it "should check basic math" do - (40+2).should eq 42 + + it "should check basic math" do + 2+2.should eq 4 end - it "should check basic spelling" do - "Field".should include('ie') + it "should check basic spelling" do + "spell".should include("ll") end - end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..76e2c71 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,18 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? - +An object holds information that methods can interavt with and manipulate. Everything in Ruby is an object. 2. What is a variable? +A variable is a place where information is stored that has a unique name identifier. They keep track of objects, and each variable is a reference to an object. 3. What is the difference between an object and a class? +Classes are created and defined, and they are define the paramters of objects. Objects are anything you can interact with in Ruby. 4. What is a String? +A string is a sequence of characters. 5. What are three messages that I can send to a string object? Hint: think methods +split, squeeze and chomp 6. What are two ways of defining a String literal? Bonus: What is the difference between them? +Single quotes and double quotes. The difference is the escape sequences you can use with each. Double quotes have more escape sequences available. \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..cd08e41 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 @@ -12,14 +12,15 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" + it "should be able to count the charaters" do + @my_string.should have(66).charaters + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @my_string.split(/\s*\.\s*/) result.should have(2).items end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + puts "encoding of #{@my_string.inspect} is #{@my_string.encoding}" end end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..61dff2d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A hash stores a whole integer while an array can hold any object. 2. When would you use an Array over a Hash and vice versa? +You would use a hash when you have a key and an object corresponding to that key, such as the key being "name" and the object being the person's name. 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is simliar to a library, and it interacts with functions to perform repeatable processes. Enumerable allows you to terminate a loop cleanly when there are no additional values. 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +You cannot inherit more than one thing in Ruby, and you get around this with mixins. 5. What is the difference between a Module and a Class? +Modules interact with functions while classes are about objects. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..209eb3c --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,19 @@ +module SimonSays + def echo(string) + @string = string + end + def shout(string) + string = string.upcase + end + def repeat(string, x=2) + string = ((string + " ") * x).strip + end + def start_of_word(string, x) + x = x-1 + string[0..x] + end + def first_word(string) + string = string.split(/\s+/) + string[0] + end +end \ No newline at end of file diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..264266f 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -44,4 +44,4 @@ it "should tell us the first word of 'oh dear' is 'oh'" do first_word("oh dear").should == "oh" end -end +end \ No newline at end of file diff --git a/week3/exercises/book.rb b/week3/exercises/book.rb index c13e4d4..1b671a6 100644 --- a/week3/exercises/book.rb +++ b/week3/exercises/book.rb @@ -1,7 +1,28 @@ class Book +<<<<<<< HEAD + attr_accessor :pages, :title + + @@library_count = 0 + + def self.library_count + @@library_count + end + + def initialize pages = 0, title="N/A" + @pages = pages + @title = title + @@library_count +=1 + end + + def happy + $global_hello = "hello" + "There are #{pages} happy pages in this book" + end +======= def pages end +>>>>>>> f7862ea7a840b4f8235c899e2c56a93c586fbd2d end \ No newline at end of file diff --git a/week3/exercises/book_spec.rb b/week3/exercises/book_spec.rb index 72bc203..393ec7b 100644 --- a/week3/exercises/book_spec.rb +++ b/week3/exercises/book_spec.rb @@ -1,10 +1,45 @@ require './book.rb' describe Book do +<<<<<<< HEAD + + before :each do + @book = Book.new 542, "Programming Ruby" + end + + context "::library_count" do + it "should tell us how many books are in our library" do + 34233.times{ Book.new 3 } + Book.library_count.should eq 34234 + end + end + + context "#pages" do + it "should have a pages" do + @book.should respond_to "pages" + end + + it "should allow us to set the number of pages" do + @book.pages.should eq 542 + end + end + + context "#title" do + it "should ket us set and get a title" do + @book.title.should eq "Programming Ruby" + end + end + + + +end + +======= it "should have a pages" do book = Book.new book.should respond_to "pages" end -end \ No newline at end of file +end +>>>>>>> f7862ea7a840b4f8235c899e2c56a93c586fbd2d diff --git a/week3/homework/.travis.yml b/week3/homework/.travis.yml new file mode 100644 index 0000000..e69de29 diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..31f3831 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,49 @@ +class Calculator + def initialize() + end + + def sum(num_array) + if num_array.length > 0 + return num_array.inject {|result,num_array| result +num_array } + else + return 0 + end + end + + def multiply(x, y) + return x*y + end + + def multiply(x, y = nil) + if y == nil + return x.inject {|result, x| result * x} + else + return (x * y) + end + end + + def pow(x, n) + if n < 0 + return pow((1/x), -n) + elsif n == 0 + return 1 + elsif n == 1 + return x + elsif n % 2 == 0 + return pow((x*x), (n/2)) + else + return x * pow((x*x), (n-1)/2) + end + end + + def fac(x) + if x > 0 + x = (1..x).to_a + return x.inject {|result, x| result *x} + else + return 1 + end + + end + +end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..e775293 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,6 +6,25 @@ Please Read: 1. What is a symbol? +<<<<<<< HEAD +A symbol in Ruby is an identifier corresponding to a name, but can be any string of characters. + +2. What is the difference between a symbol and a string? + +The object id for a symbol will always be the same, but a string's object id will return different objects. + +3. What is a block and how do I call a block? + +A block is a chunk of code that can be run as the file is loaded. You must call out these blocks with a Begin block and an End block. + +4. How do I pass a block to a method? What is the method signature? + +You define your variable, and where you want to place your block, yield and then the block name to call that block of code into that section. + +5. Where would you use regular expressions? + +You would use a regular expression if you needed to search strings for a specific pattern of symbols, and then manipulate that pattern. +======= 2. What is the difference between a symbol and a string? 3. What is a block and how do I call a block? @@ -13,3 +32,4 @@ Please Read: 4. How do I pass a block to a method? What is the method signature? 5. Where would you use regular expressions? +>>>>>>> f7862ea7a840b4f8235c899e2c56a93c586fbd2d diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..169274b 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,11 +4,22 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +The gets command will let you read lines form a file. + 2. How would you output "Hello World!" to a file called my_output.txt? +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? +This represents directories and the underlying file system. + 4. What is an IO object? +It 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 command that will execute repeated tasks. A task is declared within Rake and takes a single parameter. \ No newline at end of file diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..6f45536 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,17 @@ +class Worker + + def initialize() + end + + def self.work x = 1 + x.times.inject(nilworker){yield} + end +end + +# I attempted this, without it working, and I do not understand why: + +# def self.work x = 1 +# x.times do +# yield +# end +# end diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..9c7478d --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,39 @@ +desc "print all names in the names in the file" +task :print_names do + file_helper("names") do |line| + puts line + end +end + +desc "creates a directory called class" +task :create_class_dir do + Dir.mkdir "class" unless Dir.exists? "class" +end + +desc "create a student dir in the class dir for each student in names" +task :create_student_dirs => [:create_class_dir] do + Dir.chdir("class") + file_helper(../names) do |line| + Dir.mkdir line unless Dir.exists? line + end + Dir.chdir("..") +end + + +desc "clean up the directories we created" +task :remove_all_dirs => [:create_students_dirs] do + Dir.chdir("class") + file_helper(../names) do |line| + Dir.mkdir line unless Dir.exists? line + end + Dir.chdir("..") + Dir.rmdir("class") #if Dir.exists "class" +end + +def file_helper file_name + File.open(file_name) do |f| + f.each do |line| + yield line.chomp + end + end +end \ No newline at end of file diff --git a/week6/exercises/open_exchange_rates_conversion.gemspec b/week6/exercises/open_exchange_rates_conversion.gemspec new file mode 100644 index 0000000..e66f639 --- /dev/null +++ b/week6/exercises/open_exchange_rates_conversion.gemspec @@ -0,0 +1,14 @@ +% cat open_exchange_rates_conversion.gemspec +Gem::Specification.new do |s| + s.name = 'open_exchange_rates_conversion' + s.version = '0.0.0' + s.date = '2014-02-16' + s.summary = "Convert currency through openexchangerates.org open API" + s.description = "Convert currency through openexchangerates.org open API" + s.authors = ["John Wade"] + s.email = 'JohnRobertWade@gmail.com' + s.files = ["lib/open_exchange_rates_conversion.rb"] + s.homepage = + 'http://rubygems.org/gems/open_exchange_rates_conversion' + s.license = 'MIT' +end \ No newline at end of file diff --git a/week6/exercises/open_exchange_rates_conversion.rb b/week6/exercises/open_exchange_rates_conversion.rb new file mode 100644 index 0000000..7c6866d --- /dev/null +++ b/week6/exercises/open_exchange_rates_conversion.rb @@ -0,0 +1,6 @@ +% cat lib/open_exchange_rates_conversion.rb +class OpenExchangeRatesConversion + def self.hi + puts "Hello World!" + end +end \ No newline at end of file diff --git a/week7/class_materials/cucumber/features/converter.feature b/week7/class_materials/cucumber/features/converter.feature new file mode 100644 index 0000000..9b7416d --- /dev/null +++ b/week7/class_materials/cucumber/features/converter.feature @@ -0,0 +1,17 @@ +Feature: Converting metric + In order to pack for London + As a traveler + I want to be told the Celsius temperature in Fahrenheit + +Scenario: + Given I have entered 0 into the converter + And I select Celsius + When I press convert + Then the result should be 32.0 on the screen + +Scenario: + Given I have entered 21 into the converter + And I select Celsius + When I press convert + Then the result should be 69.8 on the screen + diff --git a/week7/class_materials/cucumber/features/step_definitions/converter.rb b/week7/class_materials/cucumber/features/step_definitions/converter.rb new file mode 100644 index 0000000..e37c629 --- /dev/null +++ b/week7/class_materials/cucumber/features/step_definitions/converter.rb @@ -0,0 +1,18 @@ +class Converter + + def initialize(unit) + @unit = unit.to_f + end + + def type=(type) + @type = type + end + + def convert + self.send("#{@type}_convertion") + end + + def Celsius_convertion + (@unit * (9.0/5.0) + 32.0).round(1) + end +end diff --git a/week7/class_materials/cucumber/features/step_definitions/converter_steps.rb b/week7/class_materials/cucumber/features/step_definitions/converter_steps.rb new file mode 100644 index 0000000..61b9aae --- /dev/null +++ b/week7/class_materials/cucumber/features/step_definitions/converter_steps.rb @@ -0,0 +1,15 @@ +Given /^I have entered (\d+) into the converter$/ do |arg1| + @converter = Converter.new(arg1) +end + +Given /^I select Celsius$/ do + @converter.type = "Celsius" +end + +When /^I press convert$/ do + @result = @converter.convert +end + +Then /^the result should be (\d+)\.(\d+) on the screen$/ do |arg1, arg2| + @result.should eq "#{arg1}.#{arg2}".to_f +end diff --git a/week7/class_materials/minitest/book.rb b/week7/class_materials/minitest/book.rb new file mode 100644 index 0000000..294444b --- /dev/null +++ b/week7/class_materials/minitest/book.rb @@ -0,0 +1,31 @@ +class Book +end + +require 'minitest/autorun' + +# Unit tests + +class TestBook < MiniTest::Unit::TestCase + def setup + @book = Book.new('test', "Mr. Test", 4) + end + + def test_that_book_can_print_pretty + assert_equal "#{@book.title} - #{@book.author} pages: #{@book.page_count}", @book.pretty_print + end +end + +# Specs + +describe Book do + before do + @book = Book.new + @book.pages = ['This is page 1', 'Page 2 is this one', 'Page 3 is short', 'Page 4 is very very very long'] + end + + describe "when counting characters" do + it "should count the characters on every page" do + @book.characters.must_equal 29 + end + end +end \ No newline at end of file diff --git a/week7/class_materials/minitest/book_answers.rb b/week7/class_materials/minitest/book_answers.rb new file mode 100644 index 0000000..7a1f45d --- /dev/null +++ b/week7/class_materials/minitest/book_answers.rb @@ -0,0 +1,19 @@ +class Book + + attr_accessor :title, :author, :page_count, :pages + + def initialize(title="titel", author="N/A", page_count=0) + @title = title + @author = author + @page_count = page_count + end + + def pretty_print + "#{title} - #{author} pages: #{page_count}" + end + + def characters + pages.inject(0){|total, p| p.size} + end + +end diff --git a/week7/class_materials/resources.txt b/week7/class_materials/resources.txt new file mode 100644 index 0000000..ab0dc70 --- /dev/null +++ b/week7/class_materials/resources.txt @@ -0,0 +1,10 @@ +Slides: http://www.slideshare.net/reneedv/week7-15273981 + +BDD: http://en.wikipedia.org/wiki/Behavior-driven_development#Principles_of_BDD +cucumber: cukes.info +cucumber wiki: https://github.com/cucumber/cucumber/wiki +Given-When-Then: https://github.com/cucumber/cucumber/wiki/Given-When-Then +Step Definitions: https://github.com/cucumber/cucumber/wiki/Step-Definitions + +MiniTest: https://github.com/seattlerb/minitest + diff --git a/week7/class_materials/test_unit/README.rdoc b/week7/class_materials/test_unit/README.rdoc new file mode 100644 index 0000000..f2cb656 --- /dev/null +++ b/week7/class_materials/test_unit/README.rdoc @@ -0,0 +1 @@ +{Teaching Gem}[https://github.com/reneedv/teaching_gem] diff --git a/week7/class_materials/week7.key b/week7/class_materials/week7.key new file mode 100644 index 0000000..871b37c Binary files /dev/null and b/week7/class_materials/week7.key differ diff --git a/week7/class_materials/week7.pdf b/week7/class_materials/week7.pdf new file mode 100644 index 0000000..121ec18 Binary files /dev/null and b/week7/class_materials/week7.pdf differ diff --git a/week7/exercises/features/converter.feature b/week7/exercises/features/converter.feature new file mode 100644 index 0000000..5e262a8 --- /dev/null +++ b/week7/exercises/features/converter.feature @@ -0,0 +1,17 @@ +Feature: Converting metric + In order to talk about the weather back home + As a traveler in London + I want to convert a Fahrenheit temperature to Celsius + +Scenario: + Given I have entered 32 into the converter + And I set the type to Fahrenheit + When I press convert + Then the result returned should be 0.0 + +Scenario: + Given I have entered 75 into the converter + And I set the type to Fahrenheit + When I press convert + Then the result returned should be 23.9 + diff --git a/week7/homework/features/pirate.feature b/week7/homework/features/pirate.feature new file mode 100644 index 0000000..7de1a0b --- /dev/null +++ b/week7/homework/features/pirate.feature @@ -0,0 +1,11 @@ +# language: en-pirate + +Ahoy matey!: Pirate Speak + I would like help to talk like a pirate + +Heave to: The mighty speaking pirate + Gangway! I have a PirateTranslator + Blimey! I say 'Hello Friend' + Aye I hit translate + Let go and haul it prints out 'Ahoy Matey' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..dad3be3 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,23 @@ +class PirateTranslator + + def initialize + end + + WORDS = { + "Hello Friend" => "Ahoy Matey" + } + + def say str + @phrase = pirate_shout(str).to_s + end + + def translate + @result = @phrase +"\n Shiber Me Timbers You Scurvey Dogs!!" + end + +private + def pirate_shout(str) + WORDS[str] + 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..faf1a7f --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a (\w+)$/ do |arg| + @translator = Kernel.const_get(arg).new +end + +Blimey /^I (\w+) '(.+)'$/ do |method, arg| + @translator.send(method, arg) +end + +Letgoandhaul /^I hit (\w+)$/ do |arg| + @result = @translator.send(arg) +end + +Letgoandhaul /^it prints out '(.+)'$/ do |arg| + @result.split("\n ").first.should == arg +end + +Letgoandhaul /^it also prints '(.+)'$/ do |arg| + @result.split("\n ").last.should == arg +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 new file mode 100644 index 0000000..25dfc2b --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -0,0 +1,124 @@ +require 'rspec/mocks/standalone' +require 'rspec/expectations' +Given /^I start a new Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new +end + +When /^I enter my name (\w+)$/ do |name| + @game.player = name +end + +Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1| + @game.welcome_player.should eq arg1 +end + +Then /^randomly chooses who goes first$/ do + [@game.player, "Computer"].should include @game.current_player +end + +Then /^who is X and who is O$/ do + TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol +end + +Given /^I have a started Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new(:player) + @game.player = "Renee" +end + +Given /^it is my turn$/ do + @game.current_player.should eq "Renee" +end + +Given /^the computer knows my name is Renee$/ do + @game.player.should eq "Renee" +end + +Then /^the computer prints "(.*?)"$/ do |arg1| + @game.should_receive(:puts).with(arg1) + @game.indicate_player_turn +end + +Then /^waits for my input of "(.*?)"$/ do |arg1| + @game.should_receive(:gets).and_return(arg1) + @game.get_player_move +end + +Given /^it is the computers turn$/ do + @game = TicTacToe.new(:computer, :O) + @game.current_player.should eq "Computer" +end + +Then /^the computer randomly chooses an open position for its move$/ do + open_spots = @game.open_spots + @com_move = @game.computer_move + open_spots.should include(@com_move) +end + +Given /^the computer is playing X$/ do + @game.computer_symbol.should eq :X +end + +Then /^the board should have an X on it$/ do + @game.current_state.should include 'X' +end + +Given /^I am playing X$/ do + @game = TicTacToe.new(:computer, :X) + @game.player_symbol.should eq :X +end + +When /^I enter a position "(.*?)" on the board$/ do |arg1| + @old_pos = @game.board[arg1.to_sym] + @game.should_receive(:get_player_move).and_return(arg1) + @game.player_move.should eq arg1.to_sym +end + +When /^"(.*?)" is not taken$/ do |arg1| + @old_pos.should eq " " +end + +Then /^it is now the computers turn$/ do + @game.current_player.should eq "Computer" +end + +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 + +Then /^I am declared the winner$/ do + @game.determine_winner + @game.player_won?.should be_true +end + +Then /^the game ends$/ do + @game.over?.should be_true +end + +Given /^there are not three symbols in a row$/ do + @game.board = { + :A1 => :X, :A2 => :O, :A3 => :X, + :B1 => :X, :B2 => :O, :B3 => :X, + :C1 => :O, :C2 => :X, :C3 => :O + } + @game.determine_winner +end + +When /^there are no open spaces left on the board$/ do + @game.spots_open?.should be_false +end + +Then /^the game is declared a draw$/ do + @game.draw?.should be_true +end + +When /^"(.*?)" is taken$/ do |arg1| + @game.board[arg1.to_sym] = :O + @taken_spot = arg1.to_sym +end + +Then /^computer should ask me for another position "(.*?)"$/ do |arg1| + @game.board[arg1.to_sym] = ' ' + @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) + @game.player_move.should eq arg1.to_sym +end 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..9a5a69e --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,109 @@ +class TicTacToe + SYMBOLS = [:X, :O] + + attr_accessor :player, :player_symbol, :computer_symbol, :board, :current_player_identity + + def initialize(first_player = nil, mark = nil) + current_player(first_player) + + case mark + when :O + @player_symbol, @computer_symbol = :O, :X + when :X + @player_symbol, @computer_symbol = :X, :O + else + symbols = SYMBOLS.shuffle + @player_symbol, @computer_symbol = symbols.first, symbols.last + end + + @board = { A1: " ", A2: " ", A3: " ", + B1: " ", B2: " ", B3: " ", + C1: " ", C2: " ", C3: " "} + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + + move = gets.chomp + end + + def player_move + begin + move = get_player_move + end until board[move.to_sym] == " " + board[move.to_sym] = @player_symbol + + @current_player_identity = "Computer" + move.to_sym + end + + def current_state + current_board = "| 1 | 2 | 3 \n" + + "A | #{board[:A1]} | #{board[:A2]} | #{board[:A3]} \n" + + "B | #{board[:B1]} | #{board[:B2]} | #{board[:B3]} \n" + + "C | #{board[:C1]} | #{board[:C2]} | #{board[:C3]} " + end + + def current_player(flag = nil) + @current_player_identity = case flag + when :player + @player + when :computer + "Computer" + when :random + [@player, "Computer"].sample + else + @current_player_identity == "Computer" ? "Computer" : @player + end + end + def spots_open? + true unless open_spots.length == 0 + end + + def closed_spots + array = [] + self.board.each { |spot, mark| array << spot.to_s if mark != " " } + array + end + + def player_won? + win_condition_met @player_symbol + end + + def computer_won? + win_condition_met @computer_symbol + end + + def win_condition_met(mark) + true if (board[:A1] == mark && board[:A2] == mark && board[:A3] == mark) || + (board[:B1] == mark && board[:B2] == mark && board[:B3] == mark) || + (board[:C1] == mark && board[:C2] == mark && board[:C3] == mark) || + (board[:A1] == mark && board[:B1] == mark && board[:C1] == mark) || + (board[:A2] == mark && board[:B2] == mark && board[:C2] == mark) || + (board[:A3] == mark && board[:B3] == mark && board[:C3] == mark) || + (board[:A1] == mark && board[:B2] == mark && board[:C3] == mark) || + (board[:C1] == mark && board[:B2] == mark && board[:A3] == mark) + end + + def draw? + true unless spots_open? || computer_won? || player_won? + end + + def over? + true if draw? or computer_won? or player_won? + end + + def determine_winner + end + + def open_spots + %w(A1 A2 A3 B1 B2 B3 C1 C2 C3) - self.closed_spots + end +end diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature new file mode 100644 index 0000000..6f3134d --- /dev/null +++ b/week7/homework/features/tic-tac-toe.feature @@ -0,0 +1,57 @@ +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 + +Scenario: Begin Game + Given I start a new Tic-Tac-Toe game + When I enter my name Renee + Then the computer welcomes me to the game with "Welcome Renee" + And randomly chooses who goes first + And who is X and who is O + +Scenario: My Turn + Given I have a started Tic-Tac-Toe game + And it is my turn + And the computer knows my name is Renee + Then the computer prints "Renee's Move:" + And waits for my input of "B2" + +Scenario: Computer's Turn + Given I have a started Tic-Tac-Toe game + And it is the computer's turn + And the computer is playing X + Then the computer randomly chooses an open position for its move + And the board should have an X on it + +Scenario: Making Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is not taken + Then the board should have an X on it + And it is now the computer's turn + +Scenario: Making Bad Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is taken + Then computer should ask me for another position "B2" + And it is now the computer's turn + +Scenario: Winning the Game + Given I have a started Tic-Tac-Toe game + And I am playing X + When there are three X's in a row + Then I am declared the winner + And the game ends + +Scenario: Game is a draw + Given I have a started Tic-Tac-Toe game + And there are not three symbols in a row + When there are no open spaces left on the board + Then the game is declared a draw + And the game ends diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb new file mode 100644 index 0000000..0535830 --- /dev/null +++ b/week7/homework/play_game.rb @@ -0,0 +1,22 @@ +require './features/step_definitions/tic-tac-toe.rb' + +@game = TicTacToe.new +puts "What is your name?" +@game.player = gets.chomp +puts @game.welcome_player + +until @game.over? + case @game.current_player + when "Computer" + @game.computer_move + when @game.player + @game.indicate_palyer_turn + @game.player_move + end + puts @game.current_state + @game.determine_winner +end + +puts "You Won!" if @game.player_won? +puts "I Won!" if @game.computer_won? +puts "DRAW!" if @game.draw? diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt new file mode 100644 index 0000000..6d2f41a --- /dev/null +++ b/week7/homework/questions.txt @@ -0,0 +1,24 @@ + +Please Read Chapters 23 and 24 DuckTyping and MetaProgramming + +Questions: +1. What is method_missing and how can it be used? + +Method_missing is a way to intercept unanswerable messages in your code and allow them to fail softly rather than have your code break and return the dreaded NoMethodError + +2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Eigenclass is a hidden class associated with each instance of instance of another class. +Singleton methods are defined on the object itself rather than in its class. + +3. When would you use DuckTypeing? How would you use it to improve your code? + +When you want to write code that is defined by what it can do and not by its class. You would use it so your code can accept information in several formats rather than always the same format. +4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +Instance methods operate on an object and has access to its instance variables, while a class method operates on a class as a whole and has no access to a particular instance's variables. + +Instance_eval can be used on any object, but class_eval can only be used to evaluate classes. +5. What is the difference between a singleton class and a singleton method? + +A singleton method allows you to create an object with a method that only it can respond to. A singleton class allows you to give certain objects theor own methods that cannot be called on by other objects. \ No newline at end of file