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

TicTacToe Final (skc) #169

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
64f0f8e
Week1 Homework
Oct 16, 2012
2076afe
updated merge conflict
Oct 17, 2012
ae0ff6a
book work
Oct 19, 2012
60b0f56
merge conflict
Oct 23, 2012
fd3ea6a
Week 2 Homework
Oct 23, 2012
d4f36b8
merge conflict resolution
Oct 29, 2012
00c4fe8
calculator tests and methods
Oct 29, 2012
03d9893
Finished Week 3 Homework
Oct 30, 2012
b2e017e
merge conflicts
Oct 31, 2012
05d7e50
improvements
Oct 31, 2012
0801c75
class exercises
Nov 6, 2012
ea99222
merge conflicts
Nov 6, 2012
1f92b44
Update week4/homework/questions.txt
skace Nov 7, 2012
8acf744
Merge branch 'master' of git://github.com/UWE-Ruby/RubyFall2012
Nov 7, 2012
55df5cf
exercises
Nov 11, 2012
1a43128
merge conflict
Nov 11, 2012
6278424
merge conflict
Nov 14, 2012
4118338
testgem
Nov 14, 2012
f32eedb
Merge branch 'master' of git://github.com/UWE-Ruby/RubyFall2012
Nov 14, 2012
659e984
spaz_gem
Nov 14, 2012
66eb406
Merge branch 'master' of git://github.com/UWE-Ruby/RubyFall2012
Nov 21, 2012
23cf240
class exercises
Nov 25, 2012
69033dc
?
Nov 25, 2012
a6fcd9b
??
Nov 25, 2012
bb66a1f
Merge branch 'master' of git://github.com/UWE-Ruby/RubyFall2012
Nov 25, 2012
2d2f3c9
pirate translator
Nov 27, 2012
327c4d3
Homework Question Answers
skace Nov 28, 2012
59a5d9e
Merge branch 'master' of git://github.com/UWE-Ruby/RubyFall2012
Nov 28, 2012
f2dff9f
Merge branch 'master' of https://github.com/scurtiss/RubyFall2012
Nov 28, 2012
9bcaa8a
??
Dec 2, 2012
660cccb
class and methods defined
Dec 3, 2012
84cab62
Redo start methods
Dec 4, 2012
27987b3
Added board and methods
Dec 5, 2012
bf42193
All cucumber scenarios pass
Dec 7, 2012
498b755
Made the game work with the play_game file
Dec 8, 2012
017831f
merge conflict
Dec 12, 2012
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spec/reports
test/tmp
test/version_tmp
tmp
.idea
*.*~
*~

Expand Down
6 changes: 4 additions & 2 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@

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

end

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

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

Expand Down
16 changes: 8 additions & 8 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ Chapter 3 Classes, Objects, and Variables
p.90-94 Strings

1. What is an object?
An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about.
Anything that is manipulated in Ruby

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.
A reference to an 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).
classes create all objects

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[]).
It's a sequence of characters

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
.chomp
.split
.squeeze

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same.
with single quotes(') or double quotes("). There are many more escape sequences and code substitution with "

8 changes: 4 additions & 4 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
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" do
@my_string.should have(@my_string.size).characters
end
it "should be able to split on the . charater" do
it "should be able to count the characters" do
@my_string.length.should eq 66
end
it "should be able to split on the . character" do
result = @my_string.split('.')
result.should have(2).items
end
Expand Down
19 changes: 8 additions & 11 deletions week2/exercises/book.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
class Book

attr_accessor :title, :pages

def initialize(title, pages)
@title = title
@pages = pages
end

def page_count
"Page count is #{@pages}"
end
attr_accessor :title
def initialize(title, page_count)
@title = title
@page_count = page_count
end
def page_count
"Page count is " + @page_count.to_s
end
end
22 changes: 11 additions & 11 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order.

And Array is indexed with positive integers and a hash is a key value pair indexed by the key object
2. When would you use an Array over a Hash and vice versa?
When the items have an inherent order I would use an array, when I want to reference the items in my collection by a name or key and their order does not matter I would use a hash.

3. What is a module? Enumerable is a built in Ruby module, what is it?
A module is a way to group code that you can use across multiple classes. Enumerable is a Ruby module that provides collection functionality; iteration, searching, and sorting. It requires an implementation of the each method.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No, multiple inheritance is not allowed in Ruby. You can include multiple modules if you wanted to mix-in different functionality into your code. Code that is related with a hierarchical nature should be subclassed (inherited). A class can only have 1 direct parent, but can have lots of ancestors.

You'd want to use arrays when you need to iterate over the integer index. Hashes are better suited for storing and looking objects up with a key
3. What is a module?
A module is a set of code that is separated by a namespace. Useful to avoid naming clashes
Enumerable is a built in Ruby module, what is it?
It's a mixin that defines many methods for iteration
4. Can you inherit more than one thing in Ruby?
No, you can only inherit from one base class
How could you get around this problem?
You can include Mixins
5. What is the difference between a Module and a Class?
A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes.
A module is a namespace and isn't instantiated. It can define classes within it however. A class is instantiated
36 changes: 18 additions & 18 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module SimonSays
def echo(st)
st
end

def shout(st)
st.upcase
end
def echo(textToEcho)
textToEcho
end

def first_word(st)
st.split.first
end
def shout(textToShout)
textToShout.upcase
end

def start_of_word(st,i)
st[0...i]
end

def repeat(st, t=2)
return "Go Away!" if t==0
([st]*t).join(' ')
end
def repeat(textToRepeat, count=2)
Array.new(count, textToRepeat + " ").inject(:+).chomp(" ")
end

def start_of_word(word, count)
word[0...count]
end

def first_word(sentence)
sentence.scan(/[\w']+/)[0]
end
end

31 changes: 12 additions & 19 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
class Calculator
def sum(array)
array.inject(0){|sum, x| sum +x}
end
def sum(numberArray)
numberArray.count == 0 ? 0 : numberArray.inject(:+)
end

def multiply(*numbers)
puts numbers.inspect
numbers.flatten.inject(:*)
end
def multiply(*numberArray)
numberArray.flatten().count == 0 ? 0 : numberArray.flatten().inject(:*)
end

def pow(base, p)
#(1...p).to_a.inject(base){|r,v| r *= base}
pow_fac(base, p)
end
def power(baseNumber, exponent)
baseNumber**exponent
end

def fac(n)
#(1..n).to_a.inject(1){|f,v| f *= v}
pow_fac(n)
end
private
def pow_fac(base=nil, p)
(1..p).to_a.inject(1){|f,v| f *= base || v}
end
def factorial(number)
number == 0 ? 1 : Math.gamma(number + 1)
end
end
28 changes: 12 additions & 16 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,39 @@

# Once the above tests pass,
# write tests and code for the following:
describe "#multiply" do
it "multiplies two numbers" do
@calculator.multiply(2,2).should eq 4
end
it "multiplies two numbers" do
@calculator.multiply(2,2).should == 4
end

it "multiplies an array of numbers" do
@calculator.multiply([2,2]).should eq 4
end
it "multiplies an array of numbers" do
@calculator.multiply([2,2,6]).should == 24
end

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

# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
it "computes the factorial of 0" do
@calculator.fac(0).should eq 1
@calculator.factorial(0).should == 1
end

it "computes the factorial of 1" do
@calculator.fac(1).should eq 1
@calculator.factorial(1).should == 1
end

it "computes the factorial of 2" do
@calculator.fac(2).should eq 2
@calculator.factorial(2).should == 2
end

it "computes the factorial of 5" do
@calculator.fac(5).should eq 120
@calculator.factorial(5).should == 120
end

it "computes the factorial of 10" do
@calculator.fac(10).should eq 3628800
@calculator.factorial(10).should == 3628800
end

end

end
22 changes: 5 additions & 17 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
A symbol is a static name or identifier.

A symbol is an identifier that doesn't change in memory
2. What is the difference between a symbol and a string?
A string is a collection of characters whereas a symbol is a static identifier. A string is not static no matter what the contents of the string are. So the strings "hello" and "hello" are two different ojects, whereas the symbol :hello and :hello are the exact same object. If you think of 1 as a FixNum or fixed number, you can think of the symbol :hello as the "FixStr" or fixed string :hello.

Two identical strings will create new instances in memory whereas two symbols will have one
3. What is a block and how do I call a block?
A block is an anonymous function, or some code snipt that you can define and then call at a later time. To call a block you can use the yield keyword.

A block is a chunk of code between {} or do end. It's called as a argument to a method or yielded inside a method
4. How do I pass a block to a method? What is the method signature?
To pass a block to a method you define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. An example of passing a block to the each method of an array:

my_array.each {|a| puts a}

Any method in Ruby can take a block. You can explicitly add a block to a method by putting an ampersand & before the variable name in the method definition. An example of this would be:

def my_method(&my_block)
my_block.call
end

It's passed as an argument after the method call: doSomething {puts 'Block'}
5. Where would you use regular expressions?
Regular expressions are used for pattern matching and replacement with strings. An example would be if I wanted to write a syntax checker for some text that checked if each sentance ended with a period, started with a space and then a capital letter.
Many places. It's used for string matching, modification and replacement
12 changes: 6 additions & 6 deletions week4/class_materials/timer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Timer
def self.time_code(n=1)
start_time = Time.now
n.times{yield}
end_time = Time.now
(end_time - start_time) / n.to_f
end
def self.time_code(n=1)
start_time = Time.now
n.times{yield}
end_time = Time.now
(end_time - start_time) / n.to_f
end
end
21 changes: 10 additions & 11 deletions week4/class_materials/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
flag.should be_true
end

it "should run our code multiple times" do
counter = 0
result = Timer.time_code(17) {counter += 1}
counter.should equal 17
end

it "should give the average time" do
Time.stub(:now).and_return(0,10)
result = Timer.time_code(10) { }
result.should be_within(0.1).of(1)
end
it "should run our code multiple times" do
counter = 0
result = Timer.time_code(17){counter += 1}
counter.should eq 17
end

it "should give the average time" do
Time.stub(:now).and_return(0,1)
result = Timer.time_code(10) { }
result.should equal 10
end
end
8 changes: 5 additions & 3 deletions week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Worker
def self.work(n=1)
n.times.inject(nil){yield}
end
def self.work(n=1)
result = nil
n.times{result = yield}
result
end
end
7 changes: 7 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
It reads files using the objects in the IO class. Specifically the File subclass. (File.open(...))
2. How would you output "Hello World!" to a file called my_output.txt?
file = File.new("my_output.txt", "w")
file.puts("Hello World!")
file.close
3. What is the Directory class and what is it used for?
The Directory class is used for handling file system directories. creating, deleting, etc...
4. What is an IO object?
An IO object is an object that encompasses all input and output related tasks such as reading from the command line and outputting to a file to name a couple examples.
5. What is rake and what is it used for? What is a rake task?
Rake stands for Ruby Make which is used to do build and deployment tasks.
23 changes: 20 additions & 3 deletions week5/exercises/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@ task :test do
puts "Hello World!"
end

task :make_class_dir do
Dir.mkdir("class")
task :names do
getNamesFromFile.each do |name|
puts name
end
end

task :output
task :createclassdir do
Dir.mkdir(:RubyClass) unless Dir.exists?(:RubyClass)
end

task :createnamedir => [:createclassdir] do

end

def getNamesFromFile(file="names")
store_names = []
f = File.open("names", "r")
f.each do |name|
store_names << name
end
f.close
return store_names
end
11 changes: 11 additions & 0 deletions week5/exercises/skier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Skier
attr_accessor :events

def initialize(events=[:gs, :downhill])
@events = events
end

def ski(event)
put "This skier is skiing #{event}"
end
end
Loading