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

tic-tac-toe from week 7 for our final Craig Adams #117

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a10acde
added values for Craig
Oct 11, 2013
46cea6d
answered the questions so i'll better understand the material
Oct 12, 2013
d411a87
made the tests pass so i'll better understand how rspec works
Oct 12, 2013
ff2b085
merging
Oct 16, 2013
f782fc1
answered HW 2 questions to reinforce what I've learned
Oct 22, 2013
4ea453c
created module to contain methods for simon says
Oct 22, 2013
8221ead
tweeks to accomodate requiring simon_says.rb
Oct 22, 2013
6f0dc5d
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Oct 23, 2013
d5e576f
answered homework 3 questions to reinforce what I read
Oct 29, 2013
62ce8b3
updated one of the homework 3 answers
Oct 29, 2013
1f0d51d
homework 3, created calculator.rb to make spec pass
Oct 29, 2013
64e4570
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Oct 29, 2013
a5d0696
answered homework 4 questions to reinforce what I've read
Nov 6, 2013
605b2cb
created worker.rb for homework 4 to make the rspec pass
Nov 6, 2013
425801d
homework worker.rb added comments with prior attempts
Nov 6, 2013
17e9252
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 6, 2013
7ebb76c
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 6, 2013
a6e19e4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 13, 2013
45ecfc4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 20, 2013
dc63986
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 23, 2013
b956587
answered questions and got pirate features to pass
Nov 26, 2013
294f148
Refined class file
Nov 26, 2013
ee122a3
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 27, 2013
1060365
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Dec 4, 2013
b6fd233
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Dec 4, 2013
4d43e96
initial checkin, 31 steps passing
Dec 5, 2013
e86e7a1
initial commit
Dec 11, 2013
93436ab
minor modifications, but still no joy
Dec 11, 2013
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
33 changes: 33 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,45 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?

An object has three components: a set of flags, some instance variables, and an associated class. In Ruby,
everthing is an object.

2. What is a variable?

A variable is use to keep track of objects. Each variable holds a reference to an object. Its type is
defined by the messages that the object referenced by the variable responds. There are different types of
variables like constant, global, instance, and class.

3. What is the difference between an object and a class?

Wow, that is a tought one. In some respects, there is no real difference between them because they are actually
the same thing at different points in time (taken from http://ruby.learncodethehardway.org/book/ex42.html). In
Ruby, everything is an object, hence there is no difference. With that said though, an object is a kind of class.
Just like a Salmon (object) is a kind of Fish (class).

A Ruby class is an object of class Class

4. What is a String?

A String is a sequence of characters. They normally hold printable characters, but they can also hold binary
data. Strings are objects of class String. Strings are often created using string literals, i.e. sequences
of characters between delimiters.

5. What are three messages that I can send to a string object? Hint: think methods

String#split
String#chomp
String#scan

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?

Single quote
Double quote

The difference between them is significant. Using single quotes, you can escape things like single characters,
e.g. 'display one escape \\'. "\\" will be replaced with a single "\". Double quotes support lots of escape
sequences. For example, \n for newline character. Also you can imbed #{expr} in double quoted strings. This
provides the ability to perform math, e.g. "#{4*5/2}", repeat sets of characters, e.g. "#{'Ho! '*3}", or use global,
class, or instance variable, e.g. "#$SAFE".

Ruby does much more processing on the string while constructing the literal when using double quotes.
11 changes: 6 additions & 5 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 split on the . charater" do
pending
result = #do something with @my_string here
it "should be able to count the charaters" do
@my_string.should have(66).characters
end
it "should be able to split on the . charater" 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"))'
@my_string.encoding.should eq (Encoding.find("UTF-8"))
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,45 @@ Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?

A hash is an indexed collection of object references. You index a hash with object types like
symbols, strings, regular expressions, etc. When you store values in a hash, you supply two objects,
the index (or key) and the entry to be stored with that key, e.g. a['salmon'] = 'fish.' You can
retrieve an object by indexing the hash with the key value you used to store it, e.g. a['salmon'].

An array holds a collection of object references where each reference occupies a position in the array.
That position is identified by a non-negative integer index that starts with 0. When you store a single
value in an array, you supply the integer position and the object for that position, e.g. a[0] = 10. To
retrieve an object from the array, you supply its position, e.g. a[0]. Arrays can also be indexed by
supplying a pair of numbers [start, count] and a range where you provide a start and end positions. The
start and end positions are seperated by two or three dots.

2. When would you use an Array over a Hash and vice versa?

You would use an array over a hash when you wanted to treat the data as stacks, sets, queues, dequeues,
and FIFO queues. Arrays are ordered while Hashes are not.

You would use a hash over an array when you want to use an object as the index.

3. What is a module? Enumerable is a built in Ruby module, what is it?

A module is a way of grouping together methods, classes, and constraints. Modules give you two major
benefits: they provide a namespace and prevent name clashes and they support mixin facility. They also
define a namespace, a sandbox in which your methods and constraints can play without having to worry
about being stepped on by other methods and constraints. Modules eliminate the need for inheritance.

Enumerable is a standard mixin, that implements a number of methods in terms of the host class's each
method. An example of a mixin is inject. This method applies a function or operation to the first two
elements in the collection and then applied the operation to the result of this computation and to the
third element and so on until all elements in the collection have been used.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?

No, Ruby is a single-inheritance language. Ruby classes can include the functionality of any number of
mixins. A mixin is like a partial class definition. This provides a controlled multiple-inheritance-like
capability.

5. What is the difference between a Module and a Class?

A Module can't have instances, but a Class can. A Module isn't a Class. A Class can contain Modules.
Modules can't be inherited, but Classes can. A module can be included in a class definition. When this
occurs, all of the module's instance methods are available as methods in the class.
23 changes: 23 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module SimonSays

def SimonSays.echo(message)
@message = message
end

def SimonSays.shout(message)
@message = message.upcase
end

def SimonSays.repeat(message, repeats = 2)
Array.new(repeats, message).join(" ")
end

def SimonSays.start_of_word(message, index)
@message = message[0,index]
end

def SimonSays.first_word(message)
@message = message.split(" ")[0]
end

end
24 changes: 13 additions & 11 deletions week2/homework/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@
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"
SimonSays.echo("hello").should == "hello"
end

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

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

it "should shout multiple words" do
shout("hello world").should == "HELLO WORLD"
SimonSays.shout("hello world").should == "HELLO WORLD"
end

it "should repeat" do
repeat("hello").should == "hello hello"
SimonSays.repeat("hello").should == "hello hello"
end

it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
SimonSays.repeat("hello", 3).should == "hello hello hello"
end

it "should return the first letter" do
start_of_word("hello", 1).should == "h"
SimonSays.start_of_word("hello", 1).should == "h"
end

it "should return the first two letters" do
start_of_word("Bob", 2).should == "Bo"
SimonSays.start_of_word("Bob", 2).should == "Bo"
end

it "should tell us the first word of 'Hello World' is 'Hello'" do
first_word("Hello World").should == "Hello"
SimonSays.first_word("Hello World").should == "Hello"
end

it "should tell us the first word of 'oh dear' is 'oh'" do
first_word("oh dear").should == "oh"
SimonSays.first_word("oh dear").should == "oh"
end
end
20 changes: 20 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Calculator

def sum(sum_arg)
sum_arg.inject(0) { |sum, a| sum + a}
end

def multiply(*product_arg)
product_arg.flatten.inject { |product, a| product * a}
end

def pow(base,power)
(base ** power)
end

def fac(fact)
return 1 if fact.zero?
fact.downto(1).inject { | product, a | product * a}
end

end
11 changes: 6 additions & 5 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
end
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
describe "#power" do
it "raises one number to the power of another number" do
p = 1
32.times{ p *= 2 }
@calculator.pow(2,32).should eq p
end
end

# http://en.wikipedia.org/wiki/Factorial
Expand All @@ -64,5 +66,4 @@
end

end

end
43 changes: 43 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,53 @@ Please Read:

1. What is a symbol?

A symbol is an identifier that corresponds to a string of characters, often a
name. A symbol is constructed for a name by proceeding the name with a colon.
A symbol can be constructed for an arbitrary string by proceeding a string
literal with a colon. A particular string or name will always generate the
same symbol, regardless of how the name is used within the program. To create
a symbol, you can use the %s delimited notation.

2. What is the difference between a symbol and a string?

A string can be changed while a symbol can not. A string is mutable. When a
new string is created, it creates a new object with a new object id. The same
symbol on the other hand, will always use the same object id.

A great discuss of the differences can be found here:
http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings

3. What is a block and how do I call a block?

A block is a chunk of code enclosed between either braces or the keywords do
and end. Typically, braces are used for blocks that fit on one line and do/end
keywords are used when the block spans multiple lines. A block can be thought
of somewhat like the body of an anonymous method. A block can also take parameters.
Those parameters appear at the beginning of the block between to vertical bars.
The body of the block is not executed when Ruby first sees it. It is saved away
to be called later.

A block is called by a method that proceeds the block. For example, lets say you
have an array ["a", "b", "c"]. A block could be called by the "each" method once
for each element in the array. Each element would be passed to the block via a
parameter, e.g. ["a", "b", "c"].each do | letter |.

4. How do I pass a block to a method? What is the method signature?

A block may be invoked within a method using the yield statement. When yield
is executed, it invokes the code in the block.

The method signature look like this:

def hi_there
yield
end

hi_there {"Hi there!"}

5. Where would you use regular expressions?

Regular expressions are great for testing a string to see if it matches a pattern.
They can be used to extract from a string the sections that match all or part of a
pattern. They can also be used to change the string, replacing parts that match a
pattern.
42 changes: 42 additions & 0 deletions week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# first attempt that works
#class Worker
# def self.work(loop_count = 0)
# if loop_count != 0
# for i in 1...loop_count
# yield
# end
# end
# yield
# end
#end
#
# 2nd attempt a little better
#class Worker
# def self.work(loop_count = 0)
# i = 1
# begin
# my_test = yield
# i += 1
# end until i > loop_count
# return my_test
# end
#end
#
# 3rd attempt a little different
#class Worker
# def self.work(loop_count = 1)
# i = 0
# while i < loop_count
# my_test = yield
# i += 1
# end
# return my_test
# end
#end

#oh, best of all
class Worker
def self.work(loop_count = 1)
(0..loop_count).inject {yield}
end
end
32 changes: 32 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?

Ruby defines a single base class called IO. This class is used to handle reading files in addition
to writing files. IO is subclassed by classes File and BasicSocket. To read a file in Ruby, you'll
read from an IO stream. To open the stream, you could invoke File.open with the filename and the file
mode string of 'r'.

2. How would you output "Hello World!" to a file called my_output.txt?

hello.rb
File.open("my_output.txt", "w") do | myfile |
myfile.puts "Hello World!"
end

ruby hello.rb

3. What is the Directory class and what is it used for?

The directory class is 'Dir'. Objects of class Dir are directory streams representing directories in
the underlying file system. They provide a variety of ways to list directories and their contents. There
are a number of methods that can be used to make and delete directories, change to a directory, or open
a directory to read its content.

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 simple Ruby build program similar to make. It is an internal DLS or Domain Specific Language
using Ruby. It is used to manage software tasks. It allows you to specify tasks and describe dependencies.
It also allows you to group tasks in a namespace.

A task is made up of a chunk of Ruby code. It begins with the keyword "task" and is followed by a symbol
which represents the name of the task. That is followed by a list of pre-requisite task(s) and a do/end
block of Ruby code. There are two types of tasks; regular and file. A task performs a specific function
in the build process.
10 changes: 10 additions & 0 deletions week7/homework/features/step_definitions/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class PirateTranslator

def initialize
@pirate_string = "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"
end

def send(*args)
return @pirate_string
end
end
Loading