Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Complete Final #129

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -34,14 +34,15 @@ Week 5
* Rake

Week 6
* Mid-term due!
* Projects intro
* Gems
* CI

Week 7
* Mid-term due!
* Cucumber
* Testing frameworks
* Refactoring

Week 8
* Metaprogramming
@@ -50,7 +51,6 @@ Week 8

Week 9
* Exceptions
* Refactoring
* Review

Week 10
3 changes: 3 additions & 0 deletions ft.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file = File.new('my_output.txt', 'w')
file.write("Hello World!")
file.close
1 change: 1 addition & 0 deletions my_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
7 changes: 7 additions & 0 deletions week1/class_materials/new_printer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe 'NamePrinter Application' do
context 'When Renee is logged in' do
it 'should say Renee' do
"Renee".should == "Renee"
end
end
end
101 changes: 54 additions & 47 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,98 @@
# encoding: utf-8
# # encoding: utf-8

describe "The Rspec ruby gem" do

context "Domain Specific Language" do
# context "Domain Specific Language" do

it "creates examples with the #it keyword" do
# it "creates examples with the #it keyword" do

# this test code passes, so this example passes
1.should eq 1
# # this test code passes, so this example passes
# 1.should eq 1

end
# end

it "has keywords like #context, and #describe to help organize the spec, or specification" do
# it "has keywords like #context, and #describe to help organize the spec, or specification" do

# test code goes here
(1+2).should eq 3
# # test code goes here
# (1+2).should eq 3

end
# end

it "has easily readable methods like #should to test any object" do
# it "has easily readable methods like #should to test any object" do

"Hello".should eq "Hello"
# "Hello".should eq "Hello"

end
# end

it "has #should_not to test for negative cases" do
# it "has #should_not to test for negative cases" do

1.should_not eq 2
# 1.should_not eq 2

end
# end

it "creates readable predicate methods" do
# 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
# # 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
# end

it "alerts you when examples fail" do
# 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
# # When this example fails,
# # it will show "expected" as 2, and "actual" as 1
# 1.should eq 2

end
# end

it "supports placeholder examples that lack code (like this one)"
# it "supports placeholder examples that lack code (like this one)"

it "requires that examples use expectations (like #should) to work properly" do
# 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 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
# # 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.
# # Lesson: It's easy to write bad tests.

end
# end

it "should count the characters in my name" do
"Renée".should have(5).characters
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
# it "should check how to spell my name" do
# "Renée".should include("ée")
# end

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 -13
(1+1-5*6/2).should eq -13
end
it "should count the characters in your name" do
pending
name = "Jordan"

name.should have(6).characters

end

it "should check basic math"
it "should check basic math" do
(2+2).should eq 4
end

it "should check basic spelling"
it "should check basic spelling" do
"Hello".should include('llo')
end

end

17 changes: 17 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,13 +3,30 @@ 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 the basic unit of an object oriented programming languages. They're used to store data and call methods assigned to them.

2. What is a variable?
A variable is a reference to a value.

3. What is the difference between an object and a class?
A class is a particular kind of object that is meant to generate "instances" (also objects) of itself. The class defines not only how the class behaves, but how its instances behave.

4. What is a String?
A string is text data. They can be assigned to variables and passed into arguments.

5. What are three messages that I can send to a string object? Hint: think methods
split(','), rstrip, to_i

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
1) Surround some text with single quotes or inside a %q( ) block (you can use any non alphanumeric character as the delimiter in the block). These do not allow string interpolation and most escape sequences
Examples:
string = 'weeee'
string = %q< weeee >
string = %q# weeee #
string
#=> "weeee"
2) Surround some text with DOUBLE quotes or inside a %Q( ) or %( ) block. This enables string interpolation and most escape sequences.
Examples
string = "It\'s math time 1+1 = #{1+1}"
string
#=> "It's math time 1+1 = 2"
9 changes: 5 additions & 4 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -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 characters" do
@my_string.should have(66).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
5 changes: 5 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -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 is an set of key and value pairs, while an array is a set of only values

2. When would you use an Array over a Hash and vice versa?
You would use an array when you have a list of an unknown number of similar objects that can be treated the same, you would use a hash when you will need to access specific objects out of the collection.

3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules contain code that can be mixed into objects with the 'include' method. They can also be used to nest classes under namespaces.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
Clases can only inherit directly from one superclass, but it also inherits for it's superclass's superclass, that superclass's superclass, so on and so forth, until you reach the BasicObject. If you need to inject more code into a class and can't put in it's superclass lineage, then mix it in with a module

5. What is the difference between a Module and a Class?
A class creates instances of itself, a module is a collection of code you can mix into objects or use as a namespace.
26 changes: 26 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SimonSays
def echo(what)
what
end

def shout(what)
what.upcase
end

def repeat(what, times = 2)
return_value = []
times.times do |x|
return_value << what
end
return_value.join(' ')
end

def start_of_word(what, number_of_letters)
what[0..(number_of_letters - 1)]
end

def first_word(what)
what.split(' ')[0]
end

end
2 changes: 1 addition & 1 deletion week2/homework/simon_says_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Hint: require needs to be able to find this file to load it
require "./simon_says.rb"
require_relative "./simon_says"

describe SimonSays do
include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)
2 changes: 1 addition & 1 deletion week3/exercises/monster.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require './named_thing.rb'
require_relative 'named_thing.rb'
class Monster
include NamedThing
attr_accessor :vulnerabilities, :dangers
28 changes: 28 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Calculator
def sum(array)
return 0 if array.empty?
array.inject(:+)
end

def multiply(*args)
args.flatten.inject(:*)
end

def pow(n, power)
return_val = 1
power.times do
return_val *= n
end
return_val
end

def fac(n)
return_val = 1
while n > 0
return_val*=n
n-=1
end
return_val
end

end
4 changes: 2 additions & 2 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -38,8 +38,8 @@

it "raises one number to the power of another number" do
p = 1
32.times{ p *= 2 }
@calculator.pow(2,32).should eq p
32.times{ p *= 2 }
@calculator.pow(2,32).should eq p
end

# http://en.wikipedia.org/wiki/Factorial
6 changes: 6 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -5,11 +5,17 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
A symbol is a unique, unchangable name.

2. What is the difference between a symbol and a string?
A symbol cannot be changed.

3. What is a block and how do I call a block?
A block is a set of code that you pass to a method. Call those code in the method definition with the 'yield' keyword.

4. How do I pass a block to a method? What is the method signature?
Put the block after the arguments in a do;end block or in between curly brackets.
The method signature is the way a method is set to accept arguments and blocks.

5. Where would you use regular expressions?
When validating a string to ensure it fits a certain format (E.G. checking to see if a string works as a URL or credit card), or isolating a part of a string that fits in a particular pattern.
Empty file added week4/class_materials/person.rb
Empty file.
11 changes: 11 additions & 0 deletions week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Worker
class << self
def work(times = 1, &block)
times.times do |x|
return yield if (x += 1) == times
yield
end
end
end

end
11 changes: 11 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,7 +3,18 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
Ruby uses the methods in the IO class, along with it's subclass File, to read files. Call File.open() or File.new() and pass the path to the file, and it'll return an IO object that can be read and altered according to the "mode" (e.g. 'w' for write) you pass it.

2. How would you output "Hello World!" to a file called my_output.txt?
file = File.new('my_output.txt', 'w')
file.write("Hello World!")
file.close

3. What is the Directory class and what is it used for?
Dir objects represent directories in the computer's file system. They can be used to display what files that exist and what they contain.

4. What is an IO object?
An IO object that can be read and written to.

5. What is rake and what is it used for? What is a rake task?
Rake is a Ruby program for creating tasks in Ruby that can be called from the terminal or scheduled with a tool such as chron.
22 changes: 22 additions & 0 deletions week5/exercises/rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ENV = Hash.new([])

desc 'lollin'
task :read do
File.open('names', 'r') do |lines|
lines.each do |line|
ENV[:names] << line
puts line
end
end
end

task :classy do
Dir.mkdir 'class'
end

task :balling => [:classy, :read] do
Dir.chdir 'class'
ENV[:names].each do |name|
Dir.mkdir name
end
end
3 changes: 3 additions & 0 deletions week7/homework/features/step_definitions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# require '../../pirate_translator.rb'
require File.join(File.dirname(__FILE__), '..', '..', 'pirate_translator')

Gangway /^I have a (\w+)$/ do |arg|
@translator = Kernel.const_get(arg).new
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require File.join(File.dirname(__FILE__), '..', '..', 'play_game')
require 'rspec/mocks/standalone'
require 'rspec/expectations'
Given /^I start a new Tic\-Tac\-Toe game$/ do
Given /^I start a new TicTacToe game$/ do
@game = TicTacToe.new
end

@@ -20,7 +21,7 @@
TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol
end

Given /^I have a started Tic\-Tac\-Toe game$/ do
Given(/^I have a started TicTacToe game$/) do
@game = TicTacToe.new(:player)
@game.player = "Renee"
end
@@ -35,21 +36,21 @@

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|
@game.should_receive(:gets).and_return(arg1)
@game.get_player_move
end

Given /^it is the computer's turn$/ do
Given /^it is the computer.s 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
open_spots = @game.open_spots.clone
@com_move = @game.computer_move
open_spots.should include(@com_move)
end
@@ -68,21 +69,23 @@
end

When /^I enter a position "(.*?)" on the board$/ do |arg1|
@old_pos = @game.board[arg1.to_sym]
# @old_pos = @game.board[arg1.to_sym]
@game.player_move(arg1)
@game.should_receive(:get_player_move).and_return(arg1)
@game.player_move.should eq arg1.to_sym
@game.player_move.should eq arg1
end

When /^"(.*?)" is not taken$/ do |arg1|
@old_pos.should eq " "
@old_pos.should eq nil
end

Then /^it is now the computer's turn$/ do
Then /^it is now the computer.s turn$/ do
@game.current_player.should eq "Computer"
end

When /^there are three X's in a row$/ do
When /^there are three X.s in a row$/ do
@game = TicTacToe.new(:computer, :X)
@game.player_symbol = :X
@game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X
end

@@ -118,7 +121,6 @@
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
@game.player_move(arg1).should eq arg1
end
18 changes: 9 additions & 9 deletions week7/homework/features/tic-tac-toe.feature
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
Feature: Tic-Tac-Toe Game
As a game player I like tic-tac-toe
Feature: TicTacToe Game
As a game player I like TicTacToe
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
Given I start a new TicTacToe 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
Given I have a started TicTacToe 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
Given I have a started TicTacToe 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
Given I have a started TicTacToe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
@@ -34,7 +34,7 @@ Scenario: Making Moves
And it is now the computer's turn

Scenario: Making Bad Moves
Given I have a started Tic-Tac-Toe game
Given I have a started TicTacToe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
@@ -43,14 +43,14 @@ Scenario: Making Bad Moves
And it is now the computer's turn

Scenario: Winning the Game
Given I have a started Tic-Tac-Toe game
Given I have a started TicTacToe 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
Given I have a started TicTacToe 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
10 changes: 10 additions & 0 deletions week7/homework/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class PirateTranslator
def say(message)
puts message
end

def translate(message = '')
"Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"
end

end
211 changes: 191 additions & 20 deletions week7/homework/play_game.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,193 @@
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

#require './features/step_definitions/tic-tac-toe_steps.rb'

class TicTacToe
# attr_accessor :current_state
attr_accessor :player
attr_accessor :player_symbol
attr_accessor :board
attr_accessor :get_player_move
attr_accessor :resolution

SYMBOLS = [:X, :O]

def initialize(turn = nil, player_symbol = nil)
@player_symbol = player_symbol
@board = Hash.new
initial_state.each {|b| @board[b] = nil }
if turn == :player
@player = "Renee"
@current_player = @player
elsif turn == :computer
@player = "Renee"
@current_player = "Computer"
end
if player_symbol == :X
@player_symbol = :X
elsif player_symbol == :O
@player_symbol = :O
end
end

# def get_player_move
# @get_player_move
# end


def welcome_player
puts "Enter your name:"
@player = "Renee"
"Welcome #{@player}"
end

def current_player
return @current_player if @current_player
@current_player = [player, "Computer"][rand(2)]
if @current_player == "Computer"
@player_symbol = :O
else
@player_symbol = :X
end
@current_player
end

def current_state
@board.values
#@state ||= ('A'..'C').map {|a| (1..3).map {|b| "#{a}#{b}" }}.flatten
end

def initial_state
('A'..'C').map {|a| (1..3).map { |b|
plot = "#{a}#{b}"
@board[plot] ? "#{plot}#{(@board[plot].to_s)}" : plot
}}.flatten
end


def open_spots
#current_state.delete_if {|a| a == 'X' || a == 'O'}
empty_spots = @board.select {|k,v| v.nil? }
empty_spots.keys
end

def spots_open?
!open_spots.empty?
end

def computer_move
@move = open_spots.sample
@board[@move] = computer_symbol.to_s
@move
end

def computer_symbol
case @player_symbol
when :X
:O
when :O
:X
else
nil
end
end


def player_move(space = nil)
if space
@move = space
else
@move ||= STDIN.gets.chomp
end
@board[@move] = player_symbol.to_s
@move
end

def indicate_player_turn
message = "#{@player}'s Move:"
puts message
message
end

def determine_winner
x_analysis, o_analysis = Hash.new(0), Hash.new(0)
xs, os = @board.select {|k,v| v == 'X' }.keys, @board.select {|k,v| v == 'O' }.keys
xs.each {|x| x_analysis[x]+=1 }
os.each {|o| o_analysis[o]+=1 }

if @board[:C1] == @board[:B2] && @board[:C1] == @board[:A3]
#@resolution = "player_won"
puts @board[:C1]
puts player_symbol
if @board[:C1] == player_symbol
@resolution = 'player_won'
else
@resolution = 'computer_won'
end
elsif @board[:C3] == @board[:B2] && @board[:C3] == @board[:A1]
if @board[:C3] == player_symbol
@resolution = 'player_won'
else
@resolution = 'computer_won'
end
elsif x_analysis.values.include?(3)
x_wins
elsif o_analysis.values.include?(3)
o_wins
elsif !spots_open?
@resolution = 'draw'
end
end

def x_wins
if player_symbol == :X
@resolution == 'player_won'
elsif player_symbol == :O
@resolution == 'computer_won'
end
end

def o_wins
if player_symbol == :O
@resolution == 'player_won'
elsif player_symbol == :X
@resolution == 'computer_won'
end
end

def player_won?
@resolution == 'player_won'
end

def computer_won?
@resolution == 'computer_won'
end

def draw?
@resolution == 'draw'
end

def over?
player_won? || computer_won? || draw?
end
end

puts "You Won!" if @game.player_won?
puts "I Won!" if @game.computer_won?
puts "DRAW!" if @game.draw?
def play
@game = TicTacToe.new
puts @game.welcome_player

until @game.over?
case @game.current_player
when "Computer"
@game.computer_move
when @game.player
@game.indicate_player_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?
end
5 changes: 5 additions & 0 deletions week7/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,7 +3,12 @@ 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 special definable method on any object that get's called when it can't find the method called. The first argument is the name of the method that failed to be found, and the rest of the arguments are the arguments passed to the missing method. It can be used to define methods with names that you won't know until runtime, such as the names of yet-unmade database tables or records.
2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?
The eigenclass is sort of a version of the class of an object that lives on the object. Singleton methods live there.
3. When would you use DuckTypeing? How would you use it to improve your code?
Use duck typing when operating on collections containing objects from different classes. Duck typing helps you to reuse code, and reduces the amount of code you have to maintain.
4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval?
A class method is called on the class, while an instance method must be called on an object and can use information of the state of that object. class_eval runs the code passed to it on the eigenclass of the object it's being called on, while instance_eval runs it's code on the object.
5. What is the difference between a singleton class and a singleton method?
Singleton class is where the class methods of are defined on a class. Singleton methods are methods defined on just an object.