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

HW 7 - TIC TAC TOE DONE :) #96

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
274c4a8
Add answers to Week 1 homework
rukky86 Jan 12, 2014
c90d44e
Complete rspec exercises and fix indents in questions.txt
rukky86 Jan 12, 2014
cdb9c92
Fix spelling in Rspec code
rukky86 Jan 12, 2014
224346c
merge conflicts
Jan 24, 2014
3015188
Add homework 3
Jan 30, 2014
6f18be9
Fix indents
Jan 30, 2014
a46a62e
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
Jan 31, 2014
c81bb7b
Add my answers homework week 4
Feb 6, 2014
71b315f
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
Feb 7, 2014
3efe102
Fetch new files
Feb 17, 2014
f111b10
Fetch week 7 materials
Feb 27, 2014
71ec7ed
Create pirate translator class
Feb 27, 2014
4585046
Merge conflicts
Feb 27, 2014
2e8876e
Add passing pirate translator class
Feb 27, 2014
926de3a
Start answering questions hw 7
Feb 27, 2014
5985782
Edit answers to hw questions 7
Feb 27, 2014
cd6d9ef
Merge conflicts
Feb 28, 2014
1cf18b8
Create tic tac toe class
Mar 4, 2014
02ef2fa
Add welcome player method and attr accessor player
Mar 4, 2014
2bd7c54
Add current_player attribute and choose random first player
Mar 4, 2014
6bacff4
First scenario passing
Mar 4, 2014
4fd9180
Change init method to accept optional starting player param
Mar 4, 2014
8ca37e0
My Turn scenario now passing
Mar 5, 2014
ed7b17d
Save progress thus far
Mar 5, 2014
ff31316
Still working on assigning symbol to player
Mar 5, 2014
b9eb62e
Still on assigning symbol
Mar 5, 2014
a7747b1
Saving progress
Mar 5, 2014
9afb798
Oops fixed a few mistakes
Mar 5, 2014
c6feb61
Now working on open spots method
Mar 5, 2014
15c6fd5
open spots method and comp move passing
Mar 5, 2014
c249489
Making Moves and Making Bad Moves passing
Mar 5, 2014
adbd51e
Winning the game all green
Mar 5, 2014
83b3771
All passing tests
Mar 5, 2014
f645436
Refactoring & cleanup - rename opposite symbol
Mar 5, 2014
3c33212
Fix typo in play_gam
Mar 6, 2014
c67bd26
Refactoring code...tests pass but game does not work right
Mar 6, 2014
159d6f7
Trouble running play_game.rb, start basics again and try to isolate i…
Mar 6, 2014
baba378
First Scenario passing, play_game works until turn
Mar 6, 2014
55dc753
indicate player turn works in cucumber and play_game
Mar 6, 2014
9c08d60
Now on third Scenario
Mar 6, 2014
39c5551
current_state works and user input can be lowecase or uppercase
Mar 6, 2014
6a46eba
cucumber passing but play game is again skipping right to declaring w…
Mar 6, 2014
dc91a7c
Play game now runs but is not ending game once 3 match
Mar 6, 2014
f685dd7
Savig progress - still one small piece not working
Mar 6, 2014
4e1dc63
All passing, now need to refactor
Mar 7, 2014
4bff977
Refactored methods, reduce noise
Mar 7, 2014
6818f0e
moved some things around, refactor some more
Mar 7, 2014
651e222
Final commit! :) play_game and cucumber both pass
Mar 7, 2014
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
46 changes: 28 additions & 18 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,47 @@
context "Domain Specific Language" do

it "creates examples with the #it keyword" do

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

end

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

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

end

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

"Hello".should eq "Hello"

end

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

1.should_not eq 2

end

it "creates readable predicate methods" do

# Integers have #zero? and #nil? predicate methods, so
# rspec automatically supports the #be_zero and #be_nil parameter to #should_not method
1.should_not be_zero
1.should_not be_nil

end

it "alerts you when examples fail" do

# When this example fails,
# it will show "expected" as 2, and "actual" as 1
1.should eq 2

1.should_not eq 2
end

it "supports placeholder examples that lack code (like this one)"
it "supports placeholder examples that lack code (like this one)" do
true
end

it "requires that examples use expectations (like #should) to work properly" do

# The following expression is false.
# However, this example PASSES because no expectation was created.
true == false

# The following line of code is correct, and would cause the example to fail:
# true.should == false

# Lesson: It's easy to write bad tests.

end

it "should count the characters in my name" do
Expand All @@ -73,6 +59,28 @@
end

context "Examples for in-class test exploration" do
<<<<<<< HEAD

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
"Rukia".should have(5).characters
end

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

it "should check basic spelling" do
"Field".should include ('ie')
end

=======
it "should know order of operations" do
# Fix the Failing Test
# Order of Operations is Please Excuse My Dear Aunt Sally:
Expand All @@ -91,6 +99,8 @@
"Field".should include('ie')
end

>>>>>>> b2040b69bb6241a745e7820ed970d0d23e0ad516
end

end

2 changes: 2 additions & 0 deletions week1/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format nested
46 changes: 38 additions & 8 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
# Rukia Fahim
# Week 1 Homework


Please read:
Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)
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?
An object is an instance or a member of a class. In Ruby, every object has a unique identifier
(object ID). You can have multiple objects of the same type or class. An object has data or state
(stored in instance variables), these can help to differentiate two objects of the same class. An
object also has defined behavior (these are the instance methods that can operate on the object's
state - usually how the object's state can be altered).

2. What is a variable?
2. What is a variable?
A variable is just a name or a label that holds a reference or pointer to an object in memory.
The name of the variable should (hopefully) hint at what it refers to. Variables are used to access
objects and manipulate them. Unlike some other languges, in Ruby, you don't need to declare a variable
before you can use it, you can assign it a value and start using it immediately. In addition to this,
variables in Ruby do not have a type meaning they can be used to point to any type of objects and can
be reassigned to point to different types of objects. Variables have scope (where they can be accessed
in a program) and a "lifetime" when they are no longer available for access.

3. What is the difference between an object and a class?
3. What is the difference between an object and a class?
Typically we model real world concepts using classes. We define a class to represent a real world "thing"
or category. A class is just a blueprint or a template for defining that category. The class defines the
object's properties/characteristics (the object will store these in instance variables) and the behavior
of the object (instance methods). When we use the class blueprint to create an actual instance this is
what we call an object or an instance of a class.

4. What is a String?
4. What is a String?
A String is an object that is typically used to represent characters of arbitrary length and sequence.
In Ruby, String objects can be created in several ways and one of the ways is using string literals which
are a sequence of characters between delimiters such as single or double quotes.

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
String.length (returns an integer - the number of characters in the string)
String.capitalize (returns a copy of the string with first character capitalized and rest to lowercase)
String.empty? (returns true if string has length of zero)

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
6. What are two ways of defining a String literal? Bonus: What is the difference between them?
Two ways you can define a String literal is within single quotes or double quotes. The type of delimiter
determines what kind of escape sequences are supported. Double quotes support a lot more types of
substitution and escape sequences including string interpolation (ex. #{expression}).
24 changes: 19 additions & 5 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Rukia Fahim
# Week 1


# encoding: utf-8

# Please make these examples all pass
Expand All @@ -9,18 +13,28 @@

describe String do
context "When a string is defined" do

before(:all) do
@my_string = "Renée is a fun teacher. Ruby is a really cool programming language"
end
it "should be able to count the charaters"
it "should be able to split on the . charater" do
pending
result = #do something with @my_string here

it "should be able to count the characters" do
string_length = @my_string.length
@my_string.should have(string_length).characters
end

it "should be able to split on the . character" do
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"))'
result = @my_string.encoding
result.should eq(Encoding.find("UTF-8"))

end

end

end

22 changes: 20 additions & 2 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
Please Read The Chapters on:
Containers, Blocks, and Iterators
Please Read The Chapters on:
Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
Both Arrays and Hashes are indexed collections of object references (they store data),
but Arrays use ordered integers as the index (starting with 0), while hashes use key-value pairs,
where key serves as the index but it can be an object of any type.

2. When would you use an Array over a Hash and vice versa?
It depends on the problem at hand. If the data is ordered naturally then I would use an Array, if
I want to store associated data I would use a Hash. They also respond to different methods or can
respond to the same methods differently.


3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules are a way of grouping together methods, classes and constants and they facilitate code
reuse and removing duplication. More specifically, they allow you to use namespaces to group
things together that may not naturally form a class and they provide a facility called mixin which
allows you to include a module within a class definition and be able to use the module's instance
methods. Enumerable is a standard mixin that allows your collectionclass to use any number of the built in
methods, it simply requires you to define an iterator for the collection.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No Ruby only supports single inheritance, but Ruby can simulate multiple inheritance using Modules
and mixins.

5. What is the difference between a Module and a Class?
Modules provide methods and constants that you can use across multiple classes, they serve as "libraries".
Classes are about objects and how they are related (a class can inherit from a parent class, etc.). You create
instances of a class but you cannot create an instance of a module.
24 changes: 24 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SimonSays

def echo words
words
end

def shout words
words.upcase
end

def repeat words, times = 2
((words+" ")* times).strip
end

def start_of_word words, letters
words[0...letters]
end

def first_word words
words.split[0]
end

end

11 changes: 6 additions & 5 deletions week2/homework/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
require "./simon_says.rb"

describe SimonSays do
include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)

include SimonSays
# Hint: Inclusion is different than SimonSays.new (read about modules)

# Hint: We are just calling methods, we are not passing a message to a SimonSays object.
it "should echo hello" do
echo("hello").should == "hello"
end

it "should echo bye" do
echo("bye").should == "bye"
end

it "should shout hello" do
shout("hello").should == "HELLO"
end

it "should shout multiple words" do
shout("hello world").should == "HELLO WORLD"
end
Expand All @@ -32,7 +33,7 @@
it "should return the first letter" do
start_of_word("hello", 1).should == "h"
end

it "should return the first two letters" do
start_of_word("Bob", 2).should == "Bo"
end
Expand Down
25 changes: 25 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

class Calculator

# accepts an array param and returns sum of each element in the array
def sum(array)
array.inject(0){|sum, x| sum +x}
end

# accepts either params or array and returns product of elements
def multiply(*nums)
nums.flatten.inject(:*)
end

# accepts two number params and returns base to the power of exponent
def pow(base, exponent)
base ** exponent
end

# accepts number param and if zero returns 1 otherwise returns product of 1 to n
def fac(n)
return 1 if n.zero?
1.upto(n).inject(:*)
end

end
58 changes: 50 additions & 8 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
Please Read:
- Chapter 6 Standard Types
- Review Blocks
- Chapter 7 Regular Expressions
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants
- Chapter 6 Standard Types
- Review Blocks
- 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? A symbol is a essentially a string that is
prefixed with a colon (ex. :id). Symbols can be used to provide
descriptive names but unlike strings, they persist in memory and each
time they are called you are using the same instance. They are
efficient way of having descriptive names while saving the space one
would use to generate a string for each naming instance. Its less work
for the memory allocator and the garbage collector. Symbols are good
for when you have a string identifier that appears often in the
code.

2. What is the difference between a symbol and a string?
2. What is the difference between a symbol and a string? If you are
using a string to refer to something more than once in the code, a
symbol would be more efficient than a string. Two strings with the
same contents are two different objects, but for any given name there
is only one Symbol object. This can save both time and memory. Also
symbols are usually short and comprised of a single word.

3. What is a block and how do I call a block?
3. What is a block and how do I call a block? A block is a chunk of
code between braces or between do- end that is treated as a single
unit and that can be passed around the program almost as if they were
parameters. In Ruby, the convention is to use the brances for short
one line blocks and the do -end for multiline blocks, however the
brances have a higher precedence. A method can call the associated
block more than once using the yield statement. Methods are not
required to call associated blocks. Blocks can even have their own
arguments.

4. How do I pass a block to a method? What is the method signature?
Blocks can be passed explicitly or implicitly. To pass a block
implicitly to the method, we use the yield keyword. The block is
defined outside of the method and the method signature does is not
affected. To call the block, the method uses the yield keyword within
the method body. Since it can be hard to tell if a block has been
passed implicitly to a method, it is easy to make the mistake of
calling yield in a method that was not passed a block, which will
raise an exception. This can be avoided by doing a quick check before
the yield and calling "yield block_given?". The other way to pass a
block is to prefix the last argument in a method signature with and
ampersand which creates a Proc object that can be executed within the
method body to execute the block, this second approach can negatively
impact performance since a proc object is instantiated on each use of
the call method.

5. Where would you use regular expressions?
5. Where would you use regular expressions? Regular expressions or
Regexp are automatically built into the Ruby language (unlike some
other languages). A regular expression is used to match a pattern
against strings and can be helpful in extracting specific portions of
a string or testing to see if a pattern exists within the given
string. It uses a sequence of control characters and letters to match
various combinations.
Loading