diff --git a/catfriend_test_gem/catfriend_test_gem-0.0.0.gem b/catfriend_test_gem/catfriend_test_gem-0.0.0.gem new file mode 100644 index 0000000..645890c Binary files /dev/null and b/catfriend_test_gem/catfriend_test_gem-0.0.0.gem differ diff --git a/catfriend_test_gem/catfriend_test_gem.gemspec b/catfriend_test_gem/catfriend_test_gem.gemspec new file mode 100644 index 0000000..ee425a3 --- /dev/null +++ b/catfriend_test_gem/catfriend_test_gem.gemspec @@ -0,0 +1,13 @@ +Gem::Specification.new do |s| + s.name = 'catfriend_test_gem' + s.version = '0.0.0' + s.date = '2013-11-25' + s. summary = "test_gem is just so testy" + s.description = "A very testy test_gem" + s.authors = ["catfriend"] + s.email = "hostahound@hotmail.com" + s.files = [ + "lib/catfriend_test_gem.rb" + ] + s.require_paths = ["lib"] + end \ No newline at end of file diff --git a/catfriend_test_gem/lib/catfriend_test_gem.rb b/catfriend_test_gem/lib/catfriend_test_gem.rb new file mode 100644 index 0000000..0463fda --- /dev/null +++ b/catfriend_test_gem/lib/catfriend_test_gem.rb @@ -0,0 +1,7 @@ +module TestGem + class Test + def self.testy? + puts "Test Gem is sooooo testy" + end + end +end \ No newline at end of file diff --git a/catfriend_test_gem/test_gem-0.0.0.gem b/catfriend_test_gem/test_gem-0.0.0.gem new file mode 100644 index 0000000..5e8feb6 Binary files /dev/null and b/catfriend_test_gem/test_gem-0.0.0.gem differ diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index 177358a..fc13f01 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -10,16 +10,16 @@ Instructions for Mid-Term submission and Git Review (10pts): - After 6pm Tuesday November 19th 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 are the three uses of the curly brackets {} in Ruby? blocks, hashes, lambdas - 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? + - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? Strings are stored using the string object. - 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? +- What is your least favorite thing about Ruby so far? rspec with git a very close second Programming Problems (10pts each): - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. diff --git a/midterm/turkey.rb b/midterm/turkey.rb new file mode 100644 index 0000000..e69de29 diff --git a/thetorturefiles b/thetorturefiles new file mode 160000 index 0000000..6294eb9 --- /dev/null +++ b/thetorturefiles @@ -0,0 +1 @@ +Subproject commit 6294eb900b7d834553a6a1d98f9fbc4c74763f06 diff --git a/week1/exercises/book.rb b/week1/exercises/book.rb new file mode 100644 index 0000000..897878d --- /dev/null +++ b/week1/exercises/book.rb @@ -0,0 +1,19 @@ +class Book + + attr_accessor :title + attr_reader :page_count + + @@book_count = 0 + + def self.library_count + @@book_count + end + + def initialize title = "Not Set", page_count = 0 + @@book_count += 1 + @page_count = page_count + @title = title + + end + +end \ No newline at end of file diff --git a/week1/exercises/book_spec.rb b/week1/exercises/book_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1e0a8ef..6eb1a30 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -77,14 +77,14 @@ # 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 -13 + (1+2-5*6/2).should eq -12 end it "should count the characters in your name" do - pending + "cheri" should have (5).characters end it "should check basic math" - + (1+3).should eq 4 it "should check basic spelling" end diff --git a/week1/exercises/rspec_spec.sublime-workspace b/week1/exercises/rspec_spec.sublime-workspace new file mode 100644 index 0000000..fb3e95f --- /dev/null +++ b/week1/exercises/rspec_spec.sublime-workspace @@ -0,0 +1,151 @@ +{ + "auto_complete": + { + "selected_items": + [ + ] + }, + "buffers": + [ + ], + "build_system": "", + "command_palette": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "console": + { + "height": 0.0, + "history": + [ + ] + }, + "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": + [ + "/Users/elizabethakari/UWRuby/RubyFall2013/week1/exercises/rspec_spec.rb" + ], + "find": + { + "height": 0.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + ], + "reverse": false, + "show_context": true, + "use_buffer2": true, + "whole_word": false, + "wrap": true + }, + "groups": + [ + { + "sheets": + [ + ] + } + ], + "incremental_find": + { + "height": 23.0 + }, + "input": + { + "height": 0.0 + }, + "layout": + { + "cells": + [ + [ + 0, + 0, + 1, + 1 + ] + ], + "cols": + [ + 0.0, + 1.0 + ], + "rows": + [ + 0.0, + 1.0 + ] + }, + "menu_visible": true, + "project": "rspec_spec.sublime-project", + "replace": + { + "height": 0.0 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "select_project": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "select_symbol": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "settings": + { + }, + "show_minimap": true, + "show_open_files": false, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 150.0, + "status_bar_visible": true, + "template_settings": + { + } +} diff --git a/week1/homework/#*merge*#30459TA8# b/week1/homework/#*merge*#30459TA8# new file mode 100644 index 0000000..47e7af9 --- /dev/null +++ b/week1/homework/#*merge*#30459TA8# @@ -0,0 +1,20 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.86-90 Strings (Strings section in Chapter 6 Standard Types) + +1. What is an object? Everything in Ruby is an object. Specifically in Ruby it is synonymous with class instance. + +2. What is a variable? A variable is something that holds data. They hold a reference to an object. + +3. What is the difference between an object and a class? The class is the category. The object is the class instance. +Every object is generated from a class. + +4. What is a String? Any text surrounded by single or double quotes, or, to put another way, sequences of characters +between delimiters. + +5. What are three messages that I can send to a string object? Hint: think methods Are you just looking for string +methods? .index, .reverse, .upcase, .downcase, .swapcase, .split and many more. + +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? Text between single quotes +and text between double quotes. Uh, the quotes. Ruby does more work with the double quotes, such as looking for things that +start with a \, and expression interpolation. diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..47e7af9 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -2,14 +2,19 @@ Please read: Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) -1. What is an object? +1. What is an object? Everything in Ruby is an object. Specifically in Ruby it is synonymous with class instance. -2. What is a variable? +2. What is a variable? A variable is something that holds data. They hold a reference to an object. -3. What is the difference between an object and a class? +3. What is the difference between an object and a class? The class is the category. The object is the class instance. +Every object is generated from a class. -4. What is a String? +4. What is a String? Any text surrounded by single or double quotes, or, to put another way, sequences of characters +between delimiters. -5. What are three messages that I can send to a string object? Hint: think methods +5. What are three messages that I can send to a string object? Hint: think methods Are you just looking for string +methods? .index, .reverse, .upcase, .downcase, .swapcase, .split and many more. -6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? Text between single quotes +and text between double quotes. Uh, the quotes. Ruby does more work with the double quotes, such as looking for things that +start with a \, and expression interpolation. diff --git a/week1/homework/questions.txt.BACKUP.30389.txt b/week1/homework/questions.txt.BACKUP.30389.txt new file mode 100644 index 0000000..a2625cc --- /dev/null +++ b/week1/homework/questions.txt.BACKUP.30389.txt @@ -0,0 +1,42 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.86-90 Strings (Strings section in Chapter 6 Standard Types) + +<<<<<<< HEAD +1. What is an object? Everything in Ruby is an object. Specifically in Ruby it is synonymous with class instance. + +2. What is a variable? A variable is something that holds data. They hold a reference to an object. + +3. What is the difference between an object and a class? The class is the category. The object is the class instance. +Every object is generated from a class. + +4. What is a String? Any text surrounded by single or double quotes, or, to put another way, sequences of characters +between delimiters. + +5. What are three messages that I can send to a string object? Hint: think methods Are you just looking for string +methods? .index, .reverse, .upcase, .downcase, .swapcase, .split and many more. + +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? Text between single quotes +and text between double quotes. Uh, the quotes. Ruby does more work with the double quotes, such as looking for things that +start with a \, and expression interpolation. +======= +1. What is an object? +An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. + +2. What is a variable? +A variable is a name for a location in memory. It can contain, or point to, any type of object. + +3. What is the difference between an object and a class? +An object is an instance of a class, or a specific thing of that class's type in memory. The class is the specifics that are common to all things of that type. The classification of a concept or a thing is a class. A specific thing or concept of a class's type in memory is an object. For example: All books have titles (Class). This book's title is "Harry Potter and the Goblet of Fire" (Object). + +4. What is a String? +A string is how Ruby understands text. It is a collection of characters (Bytes), and can be created by making an instance of the String class (String.new) or as a string literal ("",'', %Q[]). + +5. What are three messages that I can send to a string object? Hint: think methods +chomp! - removes newline characters, or the specified characters, from the end of a string +strip! - removes leading or trailing whitespace from a string +split - returns an array of strings made up of the original string separated on whitespace or the specified characters or regexp + +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 quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same. +>>>>>>> 02ccc7d94fdfc30a6d2d7e93c8bee3f07a3da618 diff --git a/week1/homework/questions.txt.BASE.30389.txt b/week1/homework/questions.txt.BASE.30389.txt new file mode 100644 index 0000000..bd581a6 --- /dev/null +++ b/week1/homework/questions.txt.BASE.30389.txt @@ -0,0 +1,15 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.86-90 Strings (Strings section in Chapter 6 Standard Types) + +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? diff --git a/week1/homework/questions.txt.LOCAL.30389.txt b/week1/homework/questions.txt.LOCAL.30389.txt new file mode 100644 index 0000000..47e7af9 --- /dev/null +++ b/week1/homework/questions.txt.LOCAL.30389.txt @@ -0,0 +1,20 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.86-90 Strings (Strings section in Chapter 6 Standard Types) + +1. What is an object? Everything in Ruby is an object. Specifically in Ruby it is synonymous with class instance. + +2. What is a variable? A variable is something that holds data. They hold a reference to an object. + +3. What is the difference between an object and a class? The class is the category. The object is the class instance. +Every object is generated from a class. + +4. What is a String? Any text surrounded by single or double quotes, or, to put another way, sequences of characters +between delimiters. + +5. What are three messages that I can send to a string object? Hint: think methods Are you just looking for string +methods? .index, .reverse, .upcase, .downcase, .swapcase, .split and many more. + +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? Text between single quotes +and text between double quotes. Uh, the quotes. Ruby does more work with the double quotes, such as looking for things that +start with a \, and expression interpolation. diff --git a/week1/homework/questions.txt.REMOTE.30389.txt b/week1/homework/questions.txt.REMOTE.30389.txt new file mode 100644 index 0000000..f7276ac --- /dev/null +++ b/week1/homework/questions.txt.REMOTE.30389.txt @@ -0,0 +1,23 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.86-90 Strings (Strings section in Chapter 6 Standard Types) + +1. What is an object? +An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. + +2. What is a variable? +A variable is a name for a location in memory. It can contain, or point to, any type of object. + +3. What is the difference between an object and a class? +An object is an instance of a class, or a specific thing of that class's type in memory. The class is the specifics that are common to all things of that type. The classification of a concept or a thing is a class. A specific thing or concept of a class's type in memory is an object. For example: All books have titles (Class). This book's title is "Harry Potter and the Goblet of Fire" (Object). + +4. What is a String? +A string is how Ruby understands text. It is a collection of characters (Bytes), and can be created by making an instance of the String class (String.new) or as a string literal ("",'', %Q[]). + +5. What are three messages that I can send to a string object? Hint: think methods +chomp! - removes newline characters, or the specified characters, from the end of a string +strip! - removes leading or trailing whitespace from a string +split - returns an array of strings made up of the original string separated on whitespace or the specified characters or regexp + +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 quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same. \ 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..14cbcdc 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,15 +12,15 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" + it "should be able to count the characters" do + @my_string.should have(@my_string.size).characters + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @my_string.split('.') result.should have(2).items end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end - diff --git a/week3/exercises/monster.rb b/week3/exercises/monster.rb index 013c3d2..a5687a8 100644 --- a/week3/exercises/monster.rb +++ b/week3/exercises/monster.rb @@ -12,3 +12,17 @@ def initialize(noc, legs, name="Monster", vul = [], dangers = []) @legs = legs end end + +require './named_thing.rb' + +class Monster + include NamedThing + attr_accessor :name, :legs, :dangers, :vulnerabilities, :nocturnal + def intialize legs, nocturnal, name= "Monster" dangers= [], vulnerabilities= [], + @legs = legs + @nocturnal = nocturnal + @dangers = dangers + @vulnerabilities = vulnerabilities + super name + + end \ No newline at end of file diff --git a/week3/exercises/vampire.rb b/week3/exercises/vampire.rb index 764adf6..89c9639 100644 --- a/week3/exercises/vampire.rb +++ b/week3/exercises/vampire.rb @@ -4,3 +4,13 @@ def initialize(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dang super(noc,legs,name,vul,dangers) end end + + +inheritance below + +require './monster.rb' +class Vampire < Monster + def intialize legs = 2, nocturnal = true, name = "Vampire", dangers = [:bites], vulnerabilities = [:garlic, :sunlight] + super + + end \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..f145fd0 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,19 @@ +class Calculator + def sum input + input.inject(0, :+) + end + + def multiply *inputs + inputs.flatten.inject(:*) + end + + def pow base, exponent + base**exponent + end + + def fac n + product =1 + 1. upto(n){|i| product *= *} + product + end +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..0bc45f6 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,6 +1,25 @@ require "#{File.dirname(__FILE__)}/calculator" -describe Calculator do +class Calculator + def sum input + input.inject(0, :+) + end + + def multiply *inputs + inputs.flatten.inject(:*) + end + + def pow base, exponent + base**exponent + end + + def fac n + product =1 + 1. upto(n){|i| product *= *} + product + end + + Benchmark.bmbm do |results| before do @calculator = Calculator.new @@ -27,6 +46,9 @@ # Once the above tests pass, # write tests and code for the following: describe "#multiply" do + + (2..2).inject(&:*) + it "multiplies two numbers" do @calculator.multiply(2,2).should eq 4 end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..8f95f51 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -4,12 +4,12 @@ Please Read: - Chapter 7 Regular Expressions - Chapter 22 The Ruby Language: basic types (symbols), variables and constants -1. What is a symbol? +1. What is a symbol? Symbols represent names and some strings inside the Ruby interpreter. -2. What is the difference between a symbol and a string? +2. What is the difference between a symbol and a string? Symbols are similar to strings, but with the key difference that symbols are immutable. They cannot be changed after assignment, whereas strings can be overwritten. -3. What is a block and how do I call a block? +3. What is a block and how do I call a block? Blocks are chunks of code between curly braces or do-end. They can be called via block.call. A method can call a block using the yield statement. -4. How do I pass a block to a method? What is the method signature? +4. How do I pass a block to a method? What is the method signature? Via the yield statement. -5. Where would you use regular expressions? +5. Where would you use regular expressions? When you wish to test a string to see whether it matches a pattern, when you want to extract from a string the sections that match all or part of a pattern, or when you want to change a string, replacing parts that match a pattern. diff --git a/week4/class_materials/classextimer.rb b/week4/class_materials/classextimer.rb new file mode 100644 index 0000000..19614a9 --- /dev/null +++ b/week4/class_materials/classextimer.rb @@ -0,0 +1,11 @@ +require './code_timer.rb' + +describe CodeTimer + +class CodeTimer + def self.time_code n=1 + start_time = Time.now + yield + Time.now - start_time + + 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..3a8febc --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,10 @@ +class Worker +def self.work n = 1 + array = Array.new n + + array.each do + yield + end +end + +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..65d8538 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -2,8 +2,8 @@ Please Read: Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ -1. How does Ruby read files? -2. How would you output "Hello World!" to a file called my_output.txt? -3. What is the Directory class and what is it used for? -4. What is an IO object? -5. What is rake and what is it used for? What is a rake task? +1. How does Ruby read files? File.open, file.read, gets, +2. How would you output "Hello World!" to a file called my_output.txt? File.open, puts "Hello World!" +3. What is the Directory class and what is it used for? Class Dir objects are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. +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 build tool. diff --git a/week7/exercises/features/Converter.rb b/week7/exercises/features/Converter.rb new file mode 100644 index 0000000..d40cf58 --- /dev/null +++ b/week7/exercises/features/Converter.rb @@ -0,0 +1,11 @@ +class Converter + attr: writer :typ +def intialize celcius_value + @value = celcius_value.to_f +end + +def convert + ((@value - 32)*(5.0/9.0)).round(1) + +end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_translator.rb b/week7/homework/features/step_definitions/pirate_translator.rb new file mode 100644 index 0000000..28153ce --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_translator.rb @@ -0,0 +1,22 @@ +class Pirate Translator + def say something + + PIRATE_WORDS = { + Hello_friend: "Ahoy Matey" + } + + end + + def translate + Pirate_lookup (@said) "\n Shiber me timbers you scurvey dog!" + + end + + private + + def pirate_lookup said + key = said.gsub(' ', ' ').downcase.to_sym + PIRATE_WORDS[key] + + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..8ab018e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -101,7 +101,7 @@ :B1 => :X, :B2 => :O, :B3 => :X, :C1 => :O, :C2 => :X, :C3 => :O } - @game.determine_winner + @unplayed_moves = [] end When /^there are no open spaces left on the board$/ do 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..b033f69 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,121 @@ +class TicTacToe + SYMBOLS = [:X, :O] + attr_accessor :player, :board + + def initialize(current_player=nil, player_s=nil) + @player = player + @current_player = current_player || [:player, :computer].sample + choose_symbols(player_s) + @board = { + :A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' ', + } + end + + def choose_symbols(player_s) ||=SYMBOLS.sample + @player_symbol = {:computer => SYMBOLS.reject{|s| s==player_s}.first, :player => player_s} + end + + def player_symbol + @player_symbol [:player] + end + + def computer_symbol + @player_symbol[:computer] + end + + def current_player + {:computer => "Computer", :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}. Wouldn't you rather play a game of solitaire?" + end + + def indicate_player_turn + puts "#{@player}'s turn:" + end + + def get_player_move + gets.chomp + end + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + puts "Pick an empty square to make your mark." + move = get_player_move,to_sym + end + @board[move] = player_symbol + @current_player = :player + move + end + + def computer_move + move = get_computer_move + @board[move] = computer_symbol + @current_player = :player + end + + def get_computer_move + @board.select{|k,v| v.to_s.strip.empty?}.collect{|k,v| k}.sample + end + + def current_state + row1 = "| #{@board[:A1]} | #{@board[:A2]} | #{@board[:A3]}" |\n + row2 = "| #{@board[:B1]} | #{@board[:B2]} | #{@board[:B3]}" |\n + row3 = "| #{@board[:C1]} | #{@board[:C2]} | #{@board[:C3]}" |\n + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row1.size+"\n"+ + row3 + "-"*row1.size+"\n"+ + end + + def who_won + player_spots = @board.select{|k,v| v==player_symbol} + computer_spots = @board.select{|k,v|==computer_symbol} + + player_marks = player_spots.collect{|k,v|} {k[0].to_sym=>k[1].to_i}} + computer_marks = computer_spots.collect{|k,v|} {k[0].to_sym=>k[1].to_i}} + @player_win = false + @computer_win = false + [:A, :B, :C].each do |l| + return if @player_win = player_marks.collect{|i| i[i}.reject}|f| f.nil?.sort == [1,2,3] + return if @computer_win = computer_marks.collect{|i| i[i}.reject}|f| f.nil?.sort == [1,2,3] + end + + [1,2,3].each do |l| + return if @player_win = player_marks.collect{|i.invert[l]}.reject{|f| f.nil?}.sort ==[:A, :B, :C] + return if @computer_win = computer_marks.collect{|i.invert[l]}.reject{|f| f.nil?}.sort ==[:A, :B, :C] + end + + return if @player_win = player_spots.keys.sort.reject{|r| ![:A1, :B2, :C3].include? r} == [:A1, :B2, :C3] + return if @player_win = player_spots.keys.sort.reject{|r| ![:A3, :B2, :C1].include? r} == [:A3, :B2, :C1] + return if @computer_win = computer_spots.keys.sort.reject{|r| ![:A1, :B2, :C3].include? r} == [:A1, :B2, :C3] + return if @computer_win = computer_spots.keys.sort.reject{|r| ![:A3, :B2, :C1].include? r} == [:A3, :B2, :C1] + end + + def player_won? + !!@player_win + end + + def computer_won? + !!@computer_win + end + + def draw? + !player_won? && !computer_won? + end + + def game_over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.collect{|k,v| k} + end +end \ No newline at end of file diff --git a/week7/homework/lib.rb b/week7/homework/lib.rb new file mode 100644 index 0000000..0463fda --- /dev/null +++ b/week7/homework/lib.rb @@ -0,0 +1,7 @@ +module TestGem + class Test + def self.testy? + puts "Test Gem is sooooo testy" + end + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..e862cc4 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -2,8 +2,16 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: -1. What is method_missing and how can it be used? +1. What is method_missing and how can it be used? Method_missing is a hook method. It is invoked when Ruby doesn't find a corresponding method by the time it runs out of superclasses. It typically results in an exception. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? -3. When would you use DuckTypeing? How would you use it to improve your code? +Eigenclass is synonymous with singleton class. Singleton methods live in the singleton class/Eigenclass that was created to define them. Which sounds weird. They also live in the world of Bridget Jones. + +3. When would you use DuckTypeing? How would you use it to improve your code? If you are using a process/file that takes too long to output a final result in a method you could pass in a something that appears to the Ruby interpreter to be like that time consuming file, but actually isn't, it could be as simple as a string, to take its place. The code would be simpler. This would speed up the process, i.e. the code. For this to work whatever is passed in instead of the original file (or whatever) must appear to the Ruby interpreter to be like the original, in other words it must walk like a duck and talk like a duck. + + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +Class methods are called on a class and instance methods are called on an instance of a class. Both instance_eval and class_eval set self for the duration of the block. However, class_eval functions as though it is in the body of a class definition, so method definitions will define instance methods. In instance_eval performs as if it were in the the singleton class of self, resulting in defined methods becoming class methods. + 5. What is the difference between a singleton class and a singleton method? +Singleton methods are specific to a particular object. When the singleton method is created is created for its particular object, Ruby creates a new anonymous class and defines that method in that class. This anonymous class is the singleton class. diff --git a/week7/homework/test_gem.gemspec b/week7/homework/test_gem.gemspec new file mode 100644 index 0000000..b5b75a6 --- /dev/null +++ b/week7/homework/test_gem.gemspec @@ -0,0 +1,13 @@ +Gem::Specification.new do |s| + s.name = 'test_gem' + s.version = '0.0.0' + s.date = '2013-11-25' + s. summary = "test_gem is just so testy" + s.description = "A very testy test_gem" + s.authors = ["catfriend"] + s.email = hostahound@hotmail.com + s.files = [ + lib/test_gem.#!/usr/bin/env ruby -wKU + ] + s.require_paths = ["lib"] + end \ No newline at end of file diff --git a/week8/class_materials/couch.rb b/week8/class_materials/couch.rb index a919c51..df4a1a0 100644 --- a/week8/class_materials/couch.rb +++ b/week8/class_materials/couch.rb @@ -9,7 +9,21 @@ def initialize(pillows, cushions, dogs) define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end + + [:pillows, :cushions, :dogs].each do |s| + define_method("#{s. to_s.gsub('s', '')colors}") do + instance_variable_get("@#{}{s}").each do |color + end + + def method_missing method_name, *args, &block + puts "You called #{method_name} with #{args}" + define_method method_name do |*args| + puts "hello there!" end +end +self.send method_name + +end # def to_str # "I am a Couch"