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

final added #111

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
1a. An object is an instance of a class.

2. What is a variable?
2a. A variable is used to keep track of an object -- it is a reference to an object.

3. What is the difference between an object and a class?
3a. An object is an instance of a class; a class is simply a combination
of a state and methods that use that state. For example, a class may be a song,
and an object would be a specific song. All songs have the same actions that
can be applied to them -- i.e. they can all be played, recorded, deleted.

4. What is a String?
4a. A string is a sequence of characters -- normally holding printable characters, but not necessarily so. They can also
contain binary data. Strings are objects of the class String.

5. What are three messages that I can send to a string object? Hint: think methods
5a. One can split a string into items by specific characters (.split),
trimming strings of repeated characters (.squeeze!), or similarly to split, using
.scan to specify the pattern of character chunks to match before dividing the
string into said chunks.

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
6a. String literals can be defined using single-quotes or double-quotes, the
difference between which are the escape sequences that are supported in each
case.
11 changes: 6 additions & 5 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 split on the . charater" do
pending
result = #do something with @my_string here
it "should be able to count the characters" do
@my_string.should have(66).characters
end
it "should be able to split on the . character" do
result = @my_string.split(/\s*\.\s*/)
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
32 changes: 32 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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?
An array is a collection of object references, indexed
by integers. A hash is also a collection of object references,
but its elements can be indexed with objects of any type, e.g.,
symbols, strings, regular expressions, etc.

2. When would you use an Array over a Hash and vice versa?
An array is useful for breaking objects down into simple elements of a list, whereas
a hash would be more advantageous for parsing through that list and indexing the
elements by the number of times they appear.
In the example where one would be looking to calculate the word frequency
in a given block of text, an array would be advantageous to split the strings
in the text into individual word elements in an array. The hash object would
be created to index by the words in the array.

3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules are a way of grouping together methods, classes, and constants. Modules
provide a namespace and prevent name clashes, as well as suppoprting the mixin facility.
Enumerable is a module that supports operations such as mapping, sorting, searching,
and ordering elements.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No. Ruby classes have only one direct parent. This can be avoided by including
a "mixin" (a partial class difinition), thereby introducing multiple-inheritance-like
capability.

5. What is the difference between a Module and a Class?
Modules cannot have instances.
21 changes: 21 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module SimonSays
def echo anything #to pass echo tests
anything
end

def shout anything #to pass shout tests
anything.upcase
end

def start_of_word (anything,n) #to pass array tests
anything[0...n]
end

def first_word anything #to pass first word test
anything.split.first
end

def repeat (anything,n=2) #to pass word repeat tests
([anything]*n).join(' ')
end
end
48 changes: 48 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Summable
def sum(whatever)
if whatever != []
whatever = whatever.flatten
whatever.inject(:+)
else
whatever = 0
end
end
end

module Empowerable
def pow(whatever, anything)
whatever**anything
end
end

module Multipliable
def multiply(whatever,anything)
if anything != nil
anything*whatever
else
anything = 1
whatever = whatever.flatten
whatever.inject(:*)*anything
end
end
end

module Factorialize
def fac(whatever)
if whatever<=1
1
else
whatever * fac(whatever-1)
end
end
end


class Calculator
include Enumerable
include Summable
include Empowerable
include Multipliable
include Factorialize
end

30 changes: 30 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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?
A symbol is something used to correspond to a
string of characters, often a name.

2. What is the difference between a symbol and a string?
Symbols of the same name are initialized and exist in
memory only once during a session of Ruby.

3. What is a block and how do I call a block?
A block is a chunk of code enclosed between either braces
or the keywords 'do' and 'end'. A block is invoked by
a Ruby iterator; it may only appear in the source adjacent
to a method call; the block is written starting on the
same line as the method's last parameter.

4. How do I pass a block to a method? What is the method signature?
A block is passed to a method when a yield statement is
executed.

5. Where would you use regular expressions?
Regular expressions would be used in tests to see whether
strings match a desired pattern, extract parts of code
that match the desired pattern, or find and replace
patterns in given strings with a desired set of characers.
20 changes: 20 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Please Read:
Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
file = File.open("file_name","r") opens an existing file named "file_name", starting at the beginning of the file, and reads it (r).

2. How would you output "Hello World!" to a file called my_output.txt?
file = File.open("my_output.txt","r+") do |text|
text << "Hello World!"
end

3. What is the Directory class and what is it used for?
The Directory class allows you to manipulate things to do with directories (folders) -- primarily navigating within directories, making new directories, and determining what is contained within a directory.

4. What is an IO object?
Ruby defines a single base class, IO, to handle input and output. An IO object is a bidrectional channel between a Ruby program and some external resource.

5. What is rake and what is it used for? What is a rake task?
Rake is a Ruby Gem. It is used for task management. A rake task is a unit of work in a Rakefile, which has its associated actions and respective prerequisites.
11 changes: 11 additions & 0 deletions week4/homework/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Worker

def self.work n=1 #sets a default value, thus making param optional

results = nil #initialize results to neutral format
n.times {results = yield} #to satisfy third test
results #show results

end

end
18 changes: 18 additions & 0 deletions week7/piratetranslator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PirateTranslator

PIRATE_VOCABULARY = {"Hello Friend" => "Ahoy Matey"}

def say(what)
@heard = listen_pirate(what)
end

def translate
@heard + "\n Shiber Me Timbers You Scurvey Dogs!!"
end

private
def listen_pirate(what)
PIRATE_VOCABULARY[what]
end
end

18 changes: 18 additions & 0 deletions week7/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

Please Read Chapters 23 and 24 DuckTyping and MetaProgramming

Questions:
1. What is method_missing and how can it be used?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good!

Method_missing is a method that receives the symbol of a non-existent method that has been called, an array of the arguments that were passed in the original call, and any block passed to the original method. It can be used to gracefully intercept any unanswerable messages that are sent to your code and return an error message that displays the problem with your input, specifically -- that the method in question is nonexistent.

2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?
An eigenclass is used to capture the creation of signleton methods and the objects for which they are defined. Singleton methods live within the eigenclass.

3. When would you use DuckTypeing? How would you use it to improve your code?
DuckTyping is used in cases where the focus is what on the code should be doing and not what the code is in and of itself. If the purpose of a class is to act like a String, then we treat it as such regardless of its innate "type". It can be used to make code interface more efficiently with larger data sets.

4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval?
Class methods can only be called on classes and instance methods can only be caled on an instance of a class. Instance_eval is used to define class methods. Class_eval is used to define instance methods.

5. What is the difference between a singleton class and a singleton method?
A singleton method is a method that is specific to a particular object; the resultant anonymous class that gets created in the interim -- behind the scenes -- is the singleton class.