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

Week 7 Homework #99

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
31a0c37
updated questions and rspec_spec.rb
Mattsface Jan 11, 2014
ccedeef
Finished homework
Mattsface Jan 11, 2014
8bdb0a4
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Jan 17, 2014
ecc5272
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Jan 17, 2014
70ca1ea
added simon_says.rb
Mattsface Jan 17, 2014
1463da1
Added homework and updated questions.txt
Mattsface Jan 18, 2014
7401d4d
updated simon_says.rb, removed the returns
Mattsface Jan 19, 2014
ee89096
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Jan 24, 2014
6ba2ff1
homework/calculator.rb
Mattsface Jan 30, 2014
024c9d8
added calculator.rb
Mattsface Jan 30, 2014
e68f791
Update rspec_spec.rb
Mattsface Jan 30, 2014
b254a0e
updated calculator
Mattsface Jan 30, 2014
eabe887
Merge branch 'master' of https://github.com/Mattsface/RubyWinter2014
Mattsface Jan 30, 2014
99a5af8
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Jan 31, 2014
83561ce
updated worker.rb
Mattsface Feb 4, 2014
41ddf17
added questions and worker.rb
Mattsface Feb 6, 2014
7adc0c6
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Feb 7, 2014
7da16b2
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Feb 14, 2014
3ef3485
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Feb 21, 2014
57bf713
Umm.. is this how the home should look? lol
Mattsface Feb 24, 2014
c7f9cda
homework
Mattsface Feb 26, 2014
9d405b4
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Feb 28, 2014
c59014b
Merge branch 'master' of git://github.com/UWE-Ruby/RubyWinter2014
Mattsface Mar 10, 2014
4739187
all done
Mattsface Mar 13, 2014
9122c5f
final all done
Mattsface Mar 13, 2014
f2ec4ac
Merge branch 'UWE-Ruby:master' into master
Mattsface Sep 1, 2022
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
27 changes: 14 additions & 13 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,23 @@
end

context "Examples for in-class test exploration" do
it "should know order of operations" do
# 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 -6
end
it "should count the characters in your name" do
"Tom".should have(3).characters
end
it "should know order of operations" do
# 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 -6
end

it "should check basic math" do
(40+2).should eq 42
it "should count the characters in your name" do
"Matt".should have(4).characters
end

it "should check basic math" do
(4+4-2).should eq 6
end

it "should check basic spelling" do
"Field".should include('ie')
it "should check basic spelling" do
"Matt".should include("Matt")
end

end
Expand Down
31 changes: 31 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,43 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?

An object is an instance of a class that is represented in memory.

2. What is a variable?

Variables holds a reference to an object or data. There are different types of variables with different scopes.

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

A class is the code or container that has properties ( methods / variable ) that can be used to create an instance of an object. A object is an instance of that class.

4. What is a String?

A string are a sequence of characters, for example "Testing", "This is a string.", and even "324234".

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

str.capitalize
This will capitalize a string

str.downcase
returns a copy of the string where every uppercase character in the string is a lowercase

str.hash
returns a hash of the string

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

Good lord, I've never seen so many ways to define a string literal in a language before. What is the best practice here?

I'll the ones I find most interesting:

%q and %Q can be used to create string literals, and the difference between them is the degree of substitution performed.

%q is the equivalent to a single-quoted delimited string, and a substitution is performed by using a single "\".

%q!This is a test string string!

%Q is the equivalent to a double-quote delimited string, and quoting the book supports a "boatload" more escape sequences. They reference the most common one, the new line sequence "\n"

%Q!Yada yada double quoted string!
17 changes: 6 additions & 11 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
# encoding: utf-8

# Please make these examples all pass
# You will need to change the 3 pending tests
# You will need to write a passing test for the first example
# (Hint: If you need help refer to the in-class exercises)
# The two tests with the pending keyword, require some ruby code to be written
# (Hint: You should do the reading on Strings first)

describe String do
context "When a string is defined" do
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(".")
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
28 changes: 11 additions & 17 deletions week2/exercises/book.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
class Book
attr_accessor :title
attr_reader :page_count

@@book_count = 0
attr_accessor :pages, :title

def self.book_count
@@book_count
end
@@library_count = 0

def initialize title = "Not Set", page_count = 0
@@book_count += 1
@page_count = page_count
def initialize pages = 0, title="N/A"
@pages = pages
@title = title
@@library_count += 1
end


def test
@test = "Hello!"
def self.library_count
@@library_count
end

def out_put_test
puts @test
puts @@book_count

def happy
"There are #{@pages} happy pages in this book"
end

end
end
58 changes: 21 additions & 37 deletions week2/exercises/book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,33 @@
require './book'
require './book.rb'

describe Book do
describe Book do


context "::book_count" do
before :each do
@book = Book.new 542, "Programming Ruby"
end

it "should count how many books have been created" do
Book.new
Book.new
Book.new
Book.book_count.should eq 3
context "::library_count" do
it "should tell us how many books we have" do
34233.times{ Book.new 3 }
Book.library_count.should eq 34234
end

end

context "::new" do

it "should set some defaults" do
Book.new.title.should eq "Not Set"
context "#pages" do
it "should have a pages" do
@book.should respond_to "pages"
end

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

end

context "#title" do

before :each do
@book = Book.new
context "#title" do
it "should let us set and get a title" do
@book.title = "Programming Ruby"
@book.title.should eq "Programming Ruby"
end
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
12 changes: 12 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ Sharing Functionality: Inheritance, Modules, and Mixins

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

An array is an ordered list of elements that are accessed using an index number. A Hash is a container of unordered elements that are stored in a map => key fashion. The values are accessed by calling the key, e.g Hash['map'].

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

Arrays are used when speed is important and elements need to be organized in an ordered fashion. Hashes are used when data needs to be accessed by a key, not the index.

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

A module is a library of reusable code that can be imported into other code. Modules can contain methods, classes, and constants. Emumerable is module that allows your classes to support many of Rubys popular methods like ".include?"

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

Yes you can, well, I'm just throwing this out there, I'm probably wrong :), but from my understanding inheritance looks like it works in a hierarchical fashion. So if you want to inherit from more than one class, you'll have to structure your classes in a manner that allow the inheritance to flow up the chain. I'm having trouble articulating this, so I'll try and draw it.

Parent Class <--- Teir 1 class <---- Teir 2 class <----- Teir 3 classs

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

A Module can't be used to create an instance.
27 changes: 27 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SimonSays

# return the string
def echo(string)
"#{string}"
end

# return string in all capitals
def shout(string)
"#{string.upcase}"
end

# repeat the string a min of 2 times, unless specified
def repeat(string, repeat=2)
("#{string} " * repeat).strip
end

# return the charaters starting from start of string to length
def start_of_word(string, length)
"#{string[0, length]}"
end

# return first word in a string using a space has a delimiter
def first_word(string)
"#{string.split(" ")[0]}"
end
end
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(*args)
args.flatten!
args.inject(0) {|sum, element| sum+element}
end

def multiply(*args)
args.flatten!
args.inject(1) {|product, element| product*element}
end
# derp
def pow(x, y)
x ** y
end

def fac(num)
# convert num to absolute value, then inject to find the factorial unless num == 0, then return 1
( (1..num.abs).inject { |factorial, n| factorial * n } unless num == 0 ) || ( 1 )
end


end



2 changes: 1 addition & 1 deletion week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "#{File.dirname(__FILE__)}/calculator"

describe Calculator do
describe Calculator do

before do
@calculator = Calculator.new
Expand Down
21 changes: 20 additions & 1 deletion week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
They are similar to a string, and can be used to represent strings or names, but unlike strings they are created only once in memory and are immutable.

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

One simple difference is that symbols are immutable and strings are mutable. Symbols are only created once in memory, unlike strings.

3. What is a block and how do I call a block?
A block is similar to a nameless function. They are always called in conjunction with a method, and wrapped in braces or a do/end. You can call a block by calling a method on a certain object, and then following up with brackets or the do/end. For example

array = [1, 2, 3, 4, 5]
array.each { |n| puts n }

Blocks are probably my favorite part of Ruby so far :)

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

You can pass a block to a method by using the yield command.

def testmethod
yield
end

testmethod { puts "This text" } => "This text"

The method signature is the method name, the arguments, and the order of the arugments.

5. Where would you use regular expressions?
There are lots of places to use a regular expression, parsing log files, trying to match certain fields in text files, and finding hidden treasure.
31 changes: 31 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,42 @@ The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?

It reads by opening up a opening and closing IO streams, which allows you to read and write data to files, standard input, and standard output.

One way to open a file is to use the File class, which is a subclass of the IO class.

file = File.new("Matts.txt", "r") # this would open the file Matts.txt for reading only

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

First, you need to open the file for writing.

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 Object Dir class is a class that allows you to manulape directories, list directories, and poke in side directories. It looks like its a great class for some shell shellish scripting :)

4. What is an IO object?

The IO object is an object that allows you to open and close streams of bytes that allow to you read and write data. It has many subclasses that include File and Directory.

5. What is rake and what is it used for? What is a rake task?

Rake is a build tool build and compile large software tools. It's very similar to make.

A rake task is a rake task... For example, Here's an example I found on the internet:

task :manipulate_files do
mkdir 'new_dir'
mv 'new_dir', 'lukasz'
chmod 0777, 'lukasz'
touch 'lukasz/wrobel.txt'
end

We could use something like this to set up an install envirement for our ruby program.



Loading