Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ spec/reports
test/tmp
test/version_tmp
tmp
<<<<<<< HEAD
*.*~
*~
=======
>>>>>>> 55106fa09d6f7a2eb0e5e798bd4797a0aec9acb1

# YARD artifacts
.yardoc
_yardoc
doc/
<<<<<<< HEAD
.DS_Store
=======
>>>>>>> 55106fa09d6f7a2eb0e5e798bd4797a0aec9acb1
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
CLASS
HEAD
RubyFall2012
============

Class materials, homework, and solutions for the Fall 2012 Ruby course
<<<<<<< HEAD
=======
Serene Careaga's midterm
=======

Fall 2012 Ruby Midterm

There will be three parts: questions, programming problems and a midterm spec. Huzzah.
>>>>>>> 55106fa09d6f7a2eb0e5e798bd4797a0aec9acb1
=======

Syllabus
============
Expand Down Expand Up @@ -57,3 +69,4 @@ Week 10
* Final due!
* Project due!
* Interesting Stuff
>>>>>>> a88274e1eccb1ad82a2f546f2d8b61c3b3a802ff
24 changes: 24 additions & 0 deletions mid_term/questions.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Instructions for Mid-Term submission and Git Review (10pts):
- Create a git repository for your answers
##done
- Add and Commit as you work through the questions and programming problems
##doing
- Your git log should reflect your work, don't just commit after you have finished working
##done
- Use .gitignore to ignore any files that are not relevant to the midterm
##done
- E-mail me your ssh public key
- I will email you back with your repository name
- Add a remote to your git repository: git@nird.us:RubyFall2012/YOURREPOSITORYNAME.git
Expand All @@ -11,6 +15,25 @@ Instructions for Mid-Term submission and Git Review (10pts):

Questions (20pts):
- What are the three uses of the curly brackets {} in Ruby?
<<<<<<< HEAD
##done
- What is a regular expression and what is a common use for them?
##done
- What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby?
##done BUT DOUBLE CHECK
- Are these two statements equivalent? Why or Why Not?
1. x, y = "hello", "hello"
2. x = y = "hello"
##done
- What is the difference between a Range and an Array?
##done
- Why would I use a Hash instead of an Array?
##done BUT DOUBLE CHECK
- What is your favorite thing about Ruby so far?
##done
- What is your least favorite thing about Ruby so far?
##done
=======
1. String interpolation: "#{1+1}"
2. Hash instantiation: {:key => "value"}
3. Block/lambda notation: {puts "hello"}
Expand All @@ -37,6 +60,7 @@ If I had data which I wanted to reference by meta-data instead of by order I wou

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

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 mid_term/wish_list_spec.rb

This file was deleted.

16 changes: 8 additions & 8 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
MY COPY FOR REALSIES

Please read:
Chapter 3 Classes, Objects, and Variables
p.90-94 Strings

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

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.
Used to keep track of objects; Holds a reference to an object; unique identifiers that bring an object to existence.

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).
An object is an instance of a class; class is like a blueprint and object is the house built from it.

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[]).
A sequence of characters contained in quotation marks.

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
Chomp, new, open

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 qoutes allow for 2 escape characters: \' and \\ . The double qouted 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 qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same.
Single quote and double quote. Differences between the two determine degree of substitution. Single-quote strings have more limited functions. Double quotes support more escape sequences (ie. \n).

10 changes: 10 additions & 0 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.

# encoding: utf-8

# Please make these examples all pass
Expand All @@ -11,12 +13,20 @@
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"
p @my_string.encoding.name
end
it "should be able to count the charaters" do
<<<<<<< HEAD
@my_string.should have(@my_string.size).characters
end
it "should be able to split on the . charater" do
result = @my_string.split('.')
=======
@my_string.should have(66).characters
end
it "should be able to split on the . charater" do
result = @my_string.split('. ')
>>>>>>> 19191e085c5cd8e019d93533ce03287bce3aa361
result.should have(2).items
end
it "should be able to give the encoding of the string" do
Expand Down
20 changes: 20 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
MINE

Please Read The Chapters on:
Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
<<<<<<< HEAD
Arrays use [] operator, hash uses {} (with key value pairs using # =>). Hashes store data in key-value pairs that can use any object as a key. (Arrays store in key-value pairs determined by index position [interger].)

2. When would you use an Array over a Hash and vice versa?
You would use an array when you simply want data stored in an indexed list. You would use a hash when you need flexibility to pair the value with a key of whatever variety you want to use.

3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules group together methods, classes and constants. They prevent name clashes and support mixins. Enumberable is a module (a standard mixin?) that implements external iterators; allows iteration over two collections in parallel.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No, restricted to single inheritance from one parent. A work around is to use mixins.

5. What is the difference between a Module and a Class?
Modules provide methods to multiple classes (functionality focused), cannot be instatiated. Classes create objects, are instantiated, use modules.


HERS
An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order.

2. When would you use an Array over a Hash and vice versa?
Expand All @@ -16,3 +35,4 @@ No, multiple inheritance is not allowed in Ruby. You can include multiple module

5. What is the difference between a Module and a Class?
A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes.

23 changes: 23 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
..
MINE
module SimonSays

def echo(greeting)
greeting
end
def shout(greeting)
greeting.upcase
end
def repeat(greeting, n=2)
("#{greeting} " * n).chop
end
def start_of_word(greeting,n)
greeting[0,n]
end
def first_word(greeting)
"#{greeting}".split(' ')[0]
end
end

HERS

def echo(st)
st
end
Expand All @@ -20,3 +42,4 @@ def repeat(st, t=2)
([st]*t).join(' ')
end
end

27 changes: 27 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
HER ANSWERS

class Calculator
<<<<<<< HEAD
def sum(array)
array.inject(0){|sum, x| sum +x}
end
Expand All @@ -22,3 +25,27 @@ def pow_fac(base=nil, p)
(1..p).to_a.inject(1){|f,v| f *= base || v}
end
end
=======
def sum(n)
##error expectation
return 0 if n.empty?
n.inject(:+)
end
def mul(n)
n.inject(:*)
end
def power(n)
n.inject(:**)
end
def factorial(n)
return 0 if n==0
(1..n).inject(:*)
end
end

stuff = Calculator.new
p stuff.sum([7,11])
p stuff.mul([8,2,1,5])
p stuff.power([2,4])
p stuff.factorial(10)
>>>>>>> f22446d7c1ea0cf75d26c958547a3d11b80f5fd7
38 changes: 38 additions & 0 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

# Once the above tests pass,
# write tests and code for the following:
<<<<<<< HEAD
describe "#multiply" do
it "multiplies two numbers" do
@calculator.multiply(2,2).should eq 4
Expand Down Expand Up @@ -64,5 +65,42 @@
end

end
=======

it "multiplies two numbers" do
@calculator.mul([1,4]).should == 4
end

it "multiplies an array of numbers" do
@calculator.mul([8,2,1,5]).should == 80
end

it "raises one number to the power of another number" do
@calculator.power([2,4]).should == 16
end


# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
>>>>>>> f22446d7c1ea0cf75d26c958547a3d11b80f5fd7

it "computes the factorial of 0" do
@calculator.factorial(0).should == 0
end

it "computes the factorial of 1" do
@calculator.factorial(1).should == 1
end
it "computes the factorial of 2" do
@calculator.factorial(2).should == 2
end
it "computes the factorial of 5" do
@calculator.factorial(5).should == 120
end
it "computes the factorial of 10" do
@calculator.factorial(10).should == 3628800
end
end
end


23 changes: 18 additions & 5 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Please Read:
- Chapter 6 Standard Types
- Review Blocks
- Chapter 7 Regular Expressions
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants


1. What is a symbol?
<<<<<<< HEAD
A symbol is a static name or identifier.

2. What is the difference between a symbol and a string?
Expand All @@ -15,6 +12,22 @@ A block is an anonymous function, or some code snipt that you can define and the

4. How do I pass a block to a method? What is the method signature?
To pass a block to a method you define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. An example of passing a block to the each method of an array:
=======
Symbols are names that start with a colon and are followed by some kind of name. Ex. serene(:awesome)
Also, the use of the symbol guarantees that that wherever it appears in the program, the value will be the same.

2. What is the difference between a symbol and a string?
A symbol assigns value whereas a string holds data sequences within quote marks.

3. What is a block and how do I call a block?
A block is a chunk of code applied to methods (like parameters) and are called between braces or between do..end.

4. How do I pass a block to a method? What is the method signature?
A block is passed into a method by placing the code in braces after the parameters. The method can call the block within it's parameters by using the yield method.

5. Where would you use regular expressions?
When there is a pattern that needs to be matched up against a string.
>>>>>>> f22446d7c1ea0cf75d26c958547a3d11b80f5fd7

my_array.each {|a| puts a}

Expand Down
16 changes: 16 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
The method gets reads a line from standard input (or from files specified on command line when script invoked) and file.gets reads the line from the file object file.

2. How would you output "Hello World!" to a file called my_output.txt?
File.open("my_output.txt", "w") do |file|
file.puts "Hello World!"
end


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

Objects from the class represent directories in the underlying file system and can list directories and contents. Ex: mkdir (creates director), chider (changes directory), .exist? (checks path and returns true if it exists)

4. What is an IO object?

A bidirectional channel between a Ruby program and an external source.


5. What is rake and what is it used for? What is a rake task?
A build tool that is used in Rails and resembles Ruby's makefile. (Wikipedia calls it a software management tool.)
A rake task is a set of procedures that are often have dependencies or can have prerequisites that depend on another task. They can also be automated with cron jobs.
Loading