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

Week7 homework in flight #100

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ad7fa53
Week one in class homework
DanWilliams7227 Jan 10, 2014
b494aaf
class work from week 1
reneedv Jan 10, 2014
201cd43
Merge pull request #16 from reneedv/master
reneedv Jan 10, 2014
ae4f39a
Merge pull request #17 from UWE-Ruby/master
reneedv Jan 12, 2014
b55089d
This is week one homework
DanWilliams7227 Jan 15, 2014
e6595c3
Week one homework again
DanWilliams7227 Jan 16, 2014
9695a13
the rspec answers from week1
reneedv Jan 17, 2014
bdf6387
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
DanWilliams7227 Jan 22, 2014
ad03998
merge master into answers
reneedv Jan 24, 2014
5d74d1c
week1 answers
reneedv Jan 24, 2014
3f90898
week 2 hw answers
reneedv Jan 24, 2014
cba2595
week3 in-class work
reneedv Jan 24, 2014
12df15b
week3 hw answers
reneedv Jan 31, 2014
1c6b6c9
Merge branch 'master' into answers
reneedv Jan 31, 2014
3b4a57f
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
DanWilliams7227 Jan 31, 2014
ac35d94
work from in-class week 4
reneedv Jan 31, 2014
00d4e74
alternative calculator that uses 1 helper method for all the methods
reneedv Jan 31, 2014
bb7334d
Updated week2 and 3 homework
DanWilliams7227 Feb 1, 2014
936f3de
week 4 homework answers
reneedv Feb 6, 2014
4c58b83
Week4 homework
DanWilliams7227 Feb 7, 2014
d1b7f20
work from in-class week 5
reneedv Feb 7, 2014
cc7389e
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
DanWilliams7227 Feb 14, 2014
cefa9f6
Merge branch 'master' of github.com:UWE-Ruby/RubyWinter2014 into answers
reneedv Feb 21, 2014
99af0a5
mid-term answers
reneedv Feb 21, 2014
da244df
Merge branch 'master' into answers
reneedv Feb 21, 2014
1ff5ed0
one line fix
reneedv Feb 21, 2014
ab8b33c
week 7 class work
reneedv Feb 21, 2014
b510de3
week7 questions
DanWilliams7227 Feb 27, 2014
cbed2f3
Week 6 and 7 homework update
DanWilliams7227 Feb 27, 2014
92c4881
Week 7 partial commit
DanWilliams7227 Feb 27, 2014
958c706
week7 homework answers
reneedv Feb 28, 2014
510de55
Merge branch 'master' into answers
reneedv Feb 28, 2014
98da443
change from review
reneedv Feb 28, 2014
24c7c71
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
DanWilliams7227 Feb 28, 2014
9438bd7
week 8 class materials and fixed exercises
reneedv Feb 28, 2014
2e155b4
Update to week 7 homework pirate.rb file
DanWilliams7227 Feb 28, 2014
757ff91
Week 7 homework updates to gem
DanWilliams7227 Mar 6, 2014
56fc0d2
Merged files for homework
DanWilliams7227 Mar 6, 2014
40ad2eb
Updated gem ruby file
DanWilliams7227 Mar 7, 2014
2cfa709
Updated TicTacToe file
DanWilliams7227 Mar 13, 2014
d74444b
Update tic-tac-toe-steps.rb
danielwilliams12 Mar 13, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.rspec
13 changes: 13 additions & 0 deletions midterm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.gem
*.rbc
.bundle
.config
coverage
InstalledFiles
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
21 changes: 21 additions & 0 deletions midterm/even_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class EvenNumber
include Comparable
attr_reader :value

def self.new(value)
return false if value.odd?
super
end

def initialize(value)
@value = value
end

def succ
EvenNumber.new(@value + 2)
end

def <=> (other)
@value <=> other.value
end
end
31 changes: 31 additions & 0 deletions midterm/even_number_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# - Write a passing rspec file called even_number_spec.rb that tests a class
# called EvenNumber.
# - The EvenNumber class should:
# - Only allow even numbers
# - Get the next even number
# - Compare even numbers
# - Generate a range of even numbers

require_relative 'even_number.rb'

describe EvenNumber do
it "should only allow even numbers" do
EvenNumber.new(5).should be_false
end

it "should get the next even number" do
EvenNumber.new(2).succ.should == EvenNumber.new(4)
end

it "should compare even numbers" do
(EvenNumber.new(2) < EvenNumber.new(4)).should be_true
end

it "should generate a range of even numbers" do
(EvenNumber.new(2)..EvenNumber.new(8)).should be_a Range
end

it "should only have EvenNumbers in the range" do
(EvenNumber.new(2)..EvenNumber.new(6)).to_a.should eq [EvenNumber.new(2), EvenNumber.new(4), EvenNumber.new(6)]
end
end
2 changes: 1 addition & 1 deletion midterm/instructions_and_questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Instructions for Mid-Term submission and Git Review (10pts):
- I will email you back with your repository name
- Add a remote to your git repository: [email protected]:RubyWinter2014/YOURREPOSITORYNAME.git
- Push your changes to the remote
- After 6pm Thursday February 20th you will not be able to push to your remote repository (or clone).
- After 6pm Tuesday November 19th 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?
Expand Down
56 changes: 56 additions & 0 deletions midterm/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Instructions for Mid-Term submission and Git Review (10pts):
- Create a git repository for your answers
- Add and Commit as you work through the questions and programming problems
- Your git log should reflect your work, don't just commit after you have finished working
- Use .gitignore to ignore any files that are not relevant to the midterm
- E-mail me your ssh public key
- I will email you back with your repository name
- Add a remote to your git repository: [email protected]:RubyFall2012/YOURREPOSITORYNAME.git
- Push your changes to the remote
- After 6pm Tuesday 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?
1. String interpolation: "#{1+1}"
2. Hash instantiation: {:key => "value"}
3. Block/lambda notation: {puts "hello"}

- What is a regular expression and what is a common use for them?
A Regular expression is a pattern used to search text. A common use for them is for verifying the format of user input, like email addresses or phone numbers.

- What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby?
A String is stored as a collection of characters (bytes) and the value of a string instance can change. A Symbol is a fixed name in ruby and is stored as an integer, so it takes up much less space than a string. A FixNum is an integer in Ruby and does not change its value. It is stored the same way as a symbol (you can think of symbols as Fixed Strings). Floats can have their value change and are stored like a string: each byte of the float is stored and can change individually (This has changed with Floats in Ruby 2.0.0 - floats with a precision of less than 32 bits are now stored like Fixnum and retain the same object_id for subsequent calls. Also, floats with greater than 32 bits in their decimal are rounded down to the closest fixed float of 32 bits. You should play with this in irb with Ruby 1.9.3 and Ruby 2.0.0 to see the difference.)

- Are these two statements equivalent? Why or Why Not?
1. x, y = "hello", "hello"
2. x = y = "hello"
No, in the first example x and y point to different objects, in the second they point to the same object. In # 2 if y changes x will change and vice versa. In # 1 if y changes x does not change and vice versa.

- What is the difference between a Range and an Array?
A Range is an object that denotes a sequence between two objects (lower/min and higher/max). It does not store every element of the sequence, only the min and max. An array is an ordered list of many objects. Each object in an array is stored and cannot necessarily be determined by the prior object in the array.

- Why would I use a Hash instead of an Array?
If I had data which I wanted to reference by meta-data instead of by order I would use a hash. For example, if I had a set of attributes common to a collection of things, I would use a hash so I could look across my data (or search) by the data's attribute dimensions.

- What is your favorite thing about Ruby so far?
.map

- What is your least favorite thing about Ruby so far?
nil.object_id => 4

Programming Problems (10pts each):
- Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber.
- The EvenNumber class should:
- Only allow even numbers
- Get the next even number
- Compare even numbers
- Generate a range of even numbers
- Make the rspec tests in wish_list_spec.rb pass by writing a WishList class
- The WishList class should:
- Mixin Enumerable
- Define each so it returns wishes as strings with their index as part of the string

Mid-Term Spec (50pts):
- Make the tests pass.


79 changes: 79 additions & 0 deletions midterm/thanksgiving_dinner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class String
def titlecase
self.gsub(/\b\w/){|f| f.upcase}
end

def humanize
self.gsub('_', ' ').titlecase
end
end

module MenuFinder
MENUS = {
:vegan => {
:diet => :vegan,
:proteins => ["Tofurkey", "Hummus"],
:veggies => [:ginger_carrots , :potatoes, :yams]
}
}
def find_menu(kind)
MENUS[kind]
end
def find_dessert
@menu[:desserts] = {:pies => [:pumkin_pie],
:other => ["Chocolate Moose"],
:molds => [:cranberry, :mango, :cherry]}
end
end

class Dinner
include MenuFinder
attr_accessor :menu, :guests, :kind
def initialize(kind)
@kind = kind
@menu = find_menu(kind)
end
def seating_chart_size
guests.inject(0){|sum,g| sum + g.size}
end

def proteins
@menu[:proteins].map{|w| w.titlecase}.join(' and ')
end

def veggies
@menu[:veggies].map{|w| w.to_s.humanize}.join(', ').gsub(/, \w*\z/){|w| w.gsub(',', ', and')}
end

def whats_for_dinner
"Tonight we have proteins #{proteins}, and veggies #{veggies}."
end
end

class ThanksgivingDinner < Dinner

def number_of_desserts
@menu[:desserts].flat_map{|k,v| v}.count
end

def pies
@menu[:desserts][:pies].map{|s| s.to_s.humanize}.join(',')
end

def other
@menu[:desserts][:other].join(',')
end

def number_of_molds
@menu[:desserts][:molds].count
end

def molds
@menu[:desserts][:molds].map{|s| s.to_s.titlecase}.join(' and ')
end

def whats_for_dessert
find_dessert
"Tonight we have #{number_of_desserts} delicious desserts: #{pies}, #{other}, and #{number_of_molds} molds: #{molds}."
end
end
24 changes: 24 additions & 0 deletions midterm/turkey.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Animal
attr_accessor :weight
def initialize(weight)
@weight = weight
end
end

class Turkey < Animal
def gobble_speak(monkey_talk)
monkey_talk.gsub(/\b\w*/) do |w|
if w[0] =~ /[A-Z]/
"Gobble"
elsif w[0] =~ /[a-z]/
"gobble"
end
end.gsub(/\b\w*'\w*\b/) do |w|
if w[0]=='G'
"Gobb'le"
else
"gobb'le"
end
end
end
end
13 changes: 13 additions & 0 deletions midterm/wish_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class WishList
include Enumerable
attr_writer :wishes

def each
@wishes.each_with_index do |w,i|
yield "#{i+1}. #{w}"
end
# @wishes.to_enum.with_index(1) do |w,i|
# yield "#{i}. #{w}"
# end
end
end
18 changes: 18 additions & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Hello World!
Another line!
Hello akjfhadf
jsdhfjkdshf


sdjfhdsf

skdjfdsjhf

dsjfhdsjfhsd
sdjkfhsdjfh

sdjfh

sdlfjsfls
sdjfhdskf
skjdhfhi from irb
19 changes: 19 additions & 0 deletions week1/exercises/roster.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#, name, email, github, twitter, hip chat
<<<<<<< HEAD
0,
1,
2,
3,
4,
5, Dan Williams, [email protected], github, hip chat
6,
7,
8,
9,
10,
=======
0, Renée, [email protected], reneedv, @nirdllc, Renee
1, Andrew Haydock, [email protected], ahaydock, N/A, Andrew
2, Armie, [email protected], aml888, lilac888, Armie
Expand All @@ -10,6 +23,7 @@
8, jack,[email protected],jackhowl,,jackhowl
9, John Wade, [email protected], THEJRRR, @THEJRRR, John Wade
10, Kevin LaFave, [email protected], kjlafave, none, @Kevin
>>>>>>> 94dfcf66c992a45f574f1894e5808847d0b6f32e
11,
12,
13, Matt Spah, [email protected], MattsFace, @MatthewsFace, Matthew Spah
Expand All @@ -19,4 +33,9 @@
17, Rukia, [email protected], rukk86, no twitter, Rukia
18,
19,
<<<<<<< HEAD
20
21
=======
20, Zack Walkingstick, [email protected], zackwalkingstick, no twitter, @zack
>>>>>>> 94dfcf66c992a45f574f1894e5808847d0b6f32e
11 changes: 10 additions & 1 deletion week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ Please read:
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 instantiantion or instance of a class.

2. What is a variable?
A variable is a pointer or reference to an object. Example: Variable -> dan = "Dan"

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

4. What is a String?
A string is one or more characters. Methods can be called on a string object.

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

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
1. "String Literal" used in interpolation with the #{interpolation of some type}
2. 'String literal' can't use interpolation.

7.
46 changes: 46 additions & 0 deletions week1/homework/questions.txt.BACKUP.2432.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Please read:
Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

<<<<<<< HEAD
1. What is an object?
An object is an instantiantion or instance of a class.

2. What is a variable?
A variable is a pointer or reference to an object. Example: Variable -> dan = "Dan"

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

4. What is a String?
A string is one or more characters. Methods can be called on a string object.

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

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
1. "String Literal" used in interpolation with the #{interpolation of some type}
2. 'String literal' can't use interpolation.

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

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.

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).

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[]).

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

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 quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same.
>>>>>>> 9438bd77063c16f253d2a0b3692ccf1a278f3a40
Loading