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

Week 7 homework #126

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions week1/class_materials/name_printer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe "NamePrinter Application" do

context "When Renee is logged in " do

it "should say Renee" do

"Renee".should eq "Renee"

end
end
end
22 changes: 16 additions & 6 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

it "alerts you when examples fail" do

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

Expand All @@ -53,7 +53,7 @@

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

# The following line of code is correct, and would cause the example to fail:
# true.should == false
Expand All @@ -77,15 +77,25 @@
# 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
"Kate".should have(4).characters
end

it "should check basic math"
it "should check basic math" do

(14*2).should eq 28

end


it "should check basic spelling" do

"Fox".should eq "Fox"

end

it "should check basic spelling"

end

Expand Down
12 changes: 7 additions & 5 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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 container for info related to a certain thing that also allow you to perform actions on it.
2. What is a variable?

Variables are storage locations for data to allow a reference to said data.
3. What is the difference between an object and a class?

Objects are manifestations of our class.
Example, BookInStock is the class that defines b1, b2, and other objects which are instantiates of it.
4. What is a String?

Strings are sequences of characters. They normally contain printable characters but that is not a requirement; they can also hold binary data. Strings are objects of class String.
5. What are three messages that I can send to a string object? Hint: think methods

split, chomp, squeeze, scan
6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
double quotes "" or here documents
9 changes: 5 additions & 4 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 count the charaters" do
@my_string.should have(66).charaters
end
it "should be able to split on the . charater" do
pending
result = #do something with @my_string here
result = @my_string.split(".") #doing something with @my_string here
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.to_s.should include("UTF-8")
end
end
end
Expand Down
Binary file modified week2/class_materials/week2.pdf
Binary file not shown.
3 changes: 1 addition & 2 deletions week2/exercises/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def initialize title = "Not Set", page_count = 0
@title = title
end


def test
@test = "Hello!"
end
Expand All @@ -24,4 +23,4 @@ def out_put_test
puts @@book_count
end

end
end
32 changes: 30 additions & 2 deletions week2/exercises/book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
require './book'
# <<<<<<< HEAD
require 'C:\Sites\RubyFall2013\week2\exercises\book.rb'

# describe Book do

# context "::book_count" do
# it "should count how many books have been created"
# Book.new
# Book.new
# Book.new
# Book.book_count.should eq 3
# end

# before :each do
# @book = Book.new("Harry Potter", 200)
# end
# it "should respond to title" do
# @book.should respond_to "title"
# end

# it "should return the page count" do
# @book.page_count.should eq "Page count is 200"
# end


# end
# =======
# require './book'

describe Book do

Expand Down Expand Up @@ -46,4 +73,5 @@

end

end
end
# >>>>>>> upstream/master
11 changes: 11 additions & 0 deletions week2/exercises/book_spec_ex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'C:\Sites\RubyFall2013\week2\exercises\book.rb'

describe Book do

it "should have a title" do
book = Book.new
book.should respond_to "title"
end


end
49 changes: 49 additions & 0 deletions week2/exercises/books_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'C:\Sites\RubyFall2013\week2\exercises\book.rb'

describe Book do


context "::book_count" do

it "should count how many books have been created" do
Book.new
Book.new
Book.new
Book.book_count.should eq 3
end

end

context "::new" do

it "should set some defaults" do
Book.new.title.should eq "Not Set"
end

it "should allow us to set the page count" do
book = Book.new "Harry Potter", 5
book.page_count.should eq 5
end

end

context "#title" do

before :each do
@book = Book.new
end

it "should have a title" do
@book.should respond_to "title"
end

it "should allow me to set the title" do
@book.title = "Snow Crash"
@book.title.should eq "Snow Crash"
end



end

end
Empty file added week2/exercises/mad_lib.rb
Empty file.
16 changes: 12 additions & 4 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

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

Arrays index with integers and hashes index with keys(objects of any type)
2. When would you use an Array over a Hash and vice versa?

I would use an array for anything I needed an ordered list of items.
Hashes are powerful and would be helpful in many scenarios including but not limited to:
Storing Country names as values with the two letter abbreviation as keys.
3. What is a module? Enumerable is a built in Ruby module, what is it?

Modules are ways of grouping methods, constants, and classes.
Enumerable is a module containing methods for all kinds of collection tasks.
4. Can you inherit more than one thing in Ruby? How could you get around this problem?

No, Ruby is a single inheritance language.
You can include the functions of any number of mixins.
5. What is the difference between a Module and a Class?
Modules cannot create instances of itself, classes can create instance objects.
You can mixin a module into a class or another module augmenting the class or module its mixed into with it's methods and constants.
Classes cannot be mixed into other classes or modules.
Classes may inherit from one other class. Modules cannot inherit from anything.
22 changes: 22 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SimonSays

def echo(word)
word
end

def shout(word)
word.upcase
end

def repeat(word, n = 2)
([word] * n).join " "
end

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

def first_word(word)
word.split(' ').first
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 'C:\Sites\RubyFall2013\week2\homework\simon_says.rb'

describe SimonSays do
include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)
Expand Down
26 changes: 26 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Calculator

def sum array
if array == []
0
else
array.inject{|n, x| n += x}
end
end

def multiply (*number)
number.flatten.inject{|n,x| n *= x}
end

def pow a, n
a ** n
end

def fac n
if n == 0
1
else
n * fac(n-1)
end
end
end
16 changes: 16 additions & 0 deletions week3/homework/calculator2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Calculator
def sum input
/result = 0
input.each do |i|
result += i
end
result/
#input.inject(0){|result, i| result+=i}
input.inject(0, :+)
end
def multiply a,b
#a*b
inputs.flatten.inject(:*)
end

end
15 changes: 9 additions & 6 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@
it "multiplies an array of numbers" do
@calculator.multiply([2,2]).should eq 4
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
end


describe "#pow" 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
describe "#factorial" do
it "computes the factorial of 0" do
Expand Down
12 changes: 8 additions & 4 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?

a symbols are constant names that you don't have to predeclare and tha re guaranteed to be uniquew
2. What is the difference between a symbol and a string?

symbols are immutable and are purely internal identifiers
strings are mutable and can be used externally
3. What is a block and how do I call a block?

A block is a chunk of code enclosed between two curly braces o the keywords do and end.
call a block directly after the parameters of a method
4. How do I pass a block to a method? What is the method signature?

put the start of the block at the end of the source line containing the method call.
method signature includes method name and the number and order of its parameters
5. Where would you use regular expressions?
In String comparison, replacement, extraction, and other scenarios.
9 changes: 9 additions & 0 deletions week4/class_materials/code_timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Class Timer

def self.time_code
start_time = Time.now
yield
Time.now - start_time
end

end
Loading