Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week 3 homework for Nathan #45

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
/*/.DS_Store
/*/*/.DS_Store
/*/doc/*
.ruby-version
.ruby-gemset
35 changes: 24 additions & 11 deletions midterm/instructions_and_questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@ Instructions for Mid-Term submission and Git Review (10pts):
- Push your changes to the remote
- After 6pm Thursday November 13th you will not be able to push to your remote repository (or clone).

Questions (20pts):
- What are the three uses of the curly brackets {} in Ruby?
- What is a regular expression and what is a common use for them?
- What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby?
- Are these two statements equivalent? Why or Why Not?
1. x, y = "hello", "hello"
2. x = y = "hello"
- What is the difference between a Range and an Array?
- Why would I use a Hash instead of an Array?
- What is your favorite thing about Ruby so far?
- What is your least favorite thing about Ruby so far?
Questions (20pts):
1 - What are the three uses of the curly brackets {} in Ruby?


2 - What is a regular expression and what is a common use for them?


3 - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby?


4 - Are these two statements equivalent? Why or Why Not?


5 - What is the difference between a Range and an Array?


6 - Why would I use a Hash instead of an Array?


7 - What is your favorite thing about Ruby so far?


8 - What is your least favorite thing about Ruby so far?


Programming Problems (10pts each):
- Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber.
Expand Down
18 changes: 0 additions & 18 deletions midterm/wish_list_spec.rb

This file was deleted.

4 changes: 4 additions & 0 deletions spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end

config.color = true

config.mock_with :rspec do |mocks|
mocks.syntax = :should
end

end
23 changes: 20 additions & 3 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'rspec/collection_matchers'
require_relative '../../spec_helper'

describe "The Rspec ruby gem" do

context "Domain Specific Language" do
Expand Down Expand Up @@ -44,7 +45,7 @@

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

end

Expand All @@ -54,10 +55,10 @@

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

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

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

Expand All @@ -81,16 +82,32 @@
(1+2-5*6/2).should eq -12
end
it "should count the characters in your name" do
<<<<<<< HEAD
"make a test to count the characters in your name"
=======
>>>>>>> d15be6bea7f47e9899c2d287761019ffd7532ba0
"Tom".should have(3).characters
end

it "should check basic math" do
<<<<<<< HEAD
"make a test to check some basic math"
(1+4+5).should eq 10
end

it "should check basic spelling" do
"make sure i is before e"
"field".should include("ie")
end

=======
(1+1).should eq 2
end

it "should check basic spelling" do
"field".should include("ie")
end
>>>>>>> d15be6bea7f47e9899c2d287761019ffd7532ba0

end
end
8 changes: 8 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ 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 thing with properties and methods that can be used to do things.

2. What is a variable?
A variable is a space in memory where you can store a value. There are different types of variables based on the kind of data stored (integer, string, bignum) and the places in your app where you want to use the app (local, class, instance, etc.)

3. What is the difference between an object and a class?
An object is an instance of a class, a class is a blueprint for objects.

4. What is a String?
A string is a collection of one or more text/numbers that has a set of properties.

5. What are three messages that I can send to a string object? Hint: think methods
Methods for strings includ Upcase, Downcase, Reverse, length, chop, clear, an_array (to_a), empty?, strip, etc

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
(a) You can define a string by using double quotation marks: s = "this is a string"
(b) YOu can also define a string with single quotation marks, but you cannot escape special characters or use interpolation with #{..}: s = 'this is another string'

15 changes: 9 additions & 6 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
@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).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"))'
encodeing #do something with @my_string here
#use helpful hint here
#helpful hint: should eq (Encoding.find("UTF-8"))'
#@my_string = @my_string.join(".")
@my_string.force_encoding("UTF-8")
result = @my_string.encoding #do something with @my_string here
result.should eq(Encoding.find("UTF-8"))
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions week2/examples/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Book

@@book_count = 0

#create a self. method so that if you change class name it cascades down
def self.book_count
@@book_count
end

attr_accessor :title, :pages, :author

def initialize title = "unknown", author: "uknown" , pages: 0
@title = title
@pages = pages
@author = author
@@book_count += 1
end

def pages
@pages
end

def book_count
@@book_count
end

end
33 changes: 33 additions & 0 deletions week2/examples/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative '../../spec_helper'
#when you say 'require' it's like running this code right here
#require knows about the .rb extension
require_relative 'book'


describe Book do

#create a new instance of book class
before :each do
@my_book = Book.new
end

it "should have a title" do
#see if it should respond to title
@my_book.should respond_to? "title"

end

it "should not have be nil" do
@my_book.title.should_not be_nil
end

it "Should have a non empty title" do
@my_book.title.size.should > 0
end

it "should let me set the title" do
@my_book.title = "Harry Potter"
@my_book.title.should eq "Harry Potter"
end

end
4 changes: 4 additions & 0 deletions week2/examples/mad_libs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

puts "enter a noun:"
noun = gets.chomp
puts "#{noun} is cool"
16 changes: 16 additions & 0 deletions week2/exercises/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

describe Book do


it "should count the books in our library" do
Book.book_count.should eq 1
end

context "::book_count" do

it "should count how many books have been created" do
Expand Down Expand Up @@ -37,11 +42,22 @@
@book.should respond_to "title"
end


it "should have a default title of unknown" do
@my_book.title should eq "unknown"
end

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

it "should allow us to set the title on a new instance" do
book = Book.new "Programming Ruby"

book.title.should eq "Programming Ruby"
end



end
Expand Down
11 changes: 9 additions & 2 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
Please Read The Chapters on:
Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins
Containers, Blocks, and Iterators -- ch4
Sharing Functionality: Inheritance, Modules, and Mixins -- ch 5

1. What is the difference between a Hash and an Array?
NTR: An array is an ordered list that can be referenced based on positions. A hash is a list of key - value pairs, which can be referenced by the key value.

2. When would you use an Array over a Hash and vice versa?
NTR: Use an array when you a list of values to store and speed / perfromance is critical. Use a hash when you have a value you want to refer to something else.

3. What is a module? Enumerable is a built in Ruby module, what is it?
NTR: A module is a collection of classes and functions that can be added into another class or module as a mixin.
Enumerable allows you to cycle through elements of elements (lists of items). YOu can use it with each or map or collect to go to every item in the list and perform some kind of check/operation.


4. Can you inherit more than one thing in Ruby? How could you get around this problem?
NTR: One child class can have one parent class. You can use modules to get around this by including them within the code with i mixin.

5. What is the difference between a Module and a Class?
NTR: At an implementation level you use the word Module instead of Class to instantiate. At an implementation level, a class is a thing, and a module is way to add more functionality to your thing without inheriting from another class. It also prevents namespace clashes by securing names within seperate realms that can then be separated with dot notation (module.function1 vs. class.funtion1).
1 change: 1 addition & 0 deletions week2/homework/sim_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

41 changes: 41 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

module SimonSays

def echo(msg)
msg
end

def hello
puts "Hello"
end

def shout(msg)
msg.upcase
end

def repeat(msg, count=2)
str = ""
count.times do
str += "#{msg} "
end
str.strip

#another way to solve this
str = []
count.times do
str.push(msg)
end
str.join(" ")
end

def start_of_word(word, length)
word[0, length]
end

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


end

Empty file added week2/homework/simonsays.rb
Empty file.
4 changes: 4 additions & 0 deletions week3/exercises/monster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ def initialize(noc, legs, name="Monster", vul = [], dangers = [])
@dangers = dangers
@legs = legs
end

def attack human
"#{name} attacked #{human.name}"
end
end
Loading