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

Includes work to date for final #115

Open
wants to merge 17 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
7 changes: 7 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
In Ruby everything is an object. An object is created either directly or indirectly by a class, and contains attributes and methods based upon it's class. It also contains the state of it's attributes.

2. What is a variable?
A variable is a reference to an object.

3. What is the difference between an object and a class?
A class is a blueprint from which individual objects can be created. Classes define the type of data and methods that an object will contain, whereas an object is an instance of the class it inherited from, containing it's own data and state.

4. What is a String?
A string is an object from the class String, that can hold printable characters or binary data.

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

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
A string literal can be defined using either single quotes or double quotes. The difference is that when using double quotes, the content of the string supports more escape sequences as well as interpolation, meaning that you can substitute the value of Ruby code, such as an instance variable, in the string if it is prepended with # and, depending on whether the code to insert is a global, class or instance variable,) wrapping the code in {}.

13 changes: 7 additions & 6 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# 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
# 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)
Expand All @@ -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 count the charaters" do
@my_string.length.should eq 66
end
it "should be able to split on the . charater" do
pending
result = #do something with @my_string here
result.should have(2).items
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
20 changes: 18 additions & 2 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
Please Read The Chapters on:
Containers, Blocks, and Iterators
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?

While there are some simlilariies between an Array and a Hash, in that they are both indexed collections of object references, the main difference is that an Array is indexed using integers, while a Hash is indexed using objects of any type, for example strings or symbols.

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

You would use an Array over a Hash when you're unable, or do not know what type of object you will be using to index the contents.
You would use an Array over a Hash when you require an integer key to index the contents.
You would use a Hash over an Array when you need to use objects as indices.
You would use a Hash over an Array when it is important to keep track of the order in which the items were entered into the Hash. (Ruby returns them in the order in which they were received).


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

A module is a way of grouping together methods, classes and contstants. They also provide a way to nampespace their contents, which is helpful in avoiding naming clashes. Modules are also usefule in that things that would not necessarialy form a class can be grouped together.

Enumerable is a mixin module, which supplies the class that requires it to with a number of methods that allow the class to traverse, sort and search it's contents, as long as the class in question provides an each(iterator) method.

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

Ruby is a single-inheritance language, so a class has only one direct parent. However, a class in Ruby can include and use any number of mixins, which are like partial class definitions. So while a Ruby class has only one direct parent, it can behave like a class that supports multiple inheritance without the dangers that surround the kind of ambiguity that multiple inheritance can provide.

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

A Module cannot have instances, as it is not a Class. A Module does not require instantiation, whereas a Class does. A Class has access to it's own intance variables, wheras a Module has access to any instance varibales of the Class it has been included in.
22 changes: 22 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SimonSays
def echo(name)
return name
end

def shout(name)
return name.upcase
end

def repeat(name, n)
Array.new(n, name).join ' '
end

def start_of_word(name, n)
@frag = name.scan /\w/
return @frag[0...n].join''
end

def first_word(name)
return name.split(" ")[0]
end
end
10 changes: 5 additions & 5 deletions week2/homework/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

describe SimonSays do
include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)

# Hint: We are just calling methods, we are not passing a message to a SimonSays object.
it "should echo hello" do
echo("hello").should == "hello"
end

it "should echo bye" do
echo("bye").should == "bye"
end

it "should shout hello" do
shout("hello").should == "HELLO"
end

it "should shout multiple words" do
shout("hello world").should == "HELLO WORLD"
end

it "should repeat" do
repeat("hello").should == "hello hello"
repeat("hello", 2).should == "hello hello"
end

it "should repeat a number of times" do
Expand All @@ -32,7 +32,7 @@
it "should return the first letter" do
start_of_word("hello", 1).should == "h"
end

it "should return the first two letters" do
start_of_word("Bob", 2).should == "Bo"
end
Expand Down
23 changes: 23 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Calculator

def sum *values
values.flatten!
values.inject(0) { |val, i| val += i }
end

def multiply *values
values.flatten!
values.inject(1) { |val, i| val *= i }
end

def pow base, multiplier
values = Array.new multiplier, base
values.flatten!
values.inject(1) {|val, i| val *= i }
end

def fac value
value == 0 ? 1 : value * fac(value-1)
end

end
23 changes: 22 additions & 1 deletion week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
Please Read:
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 an identifier that you use to represent to a string or a name.

2. What is the difference between a symbol and a string?
Strings are immutable, while symbols are not. Also, symbols can accept interpolation, where a string literal cannot.

3. What is a block and how do I call a block?
A block is a set of statements and expressions that are wrapped in either braces or a do/end pair. The block immediately follows a method invocation, for example, after the .each method is calling a block here:

[1,2,3,4].each do |value|
#block goes here
end

4. How do I pass a block to a method? What is the method signature?
You pass a block to a method by using the yield statement in a method that has the same name as the block. For example:
def test
yield
end
test{ puts "Hello world"}

You can also pass the block into a method by passing it as the last argument of the method, and by prepending & to the block name. For example:

def test(&block)
block.call
end
test { puts "Hello World!"}


5. Where would you use regular expressions?
Anywhere that we would want to match, extract or changes a string based upon a given pattern.
9 changes: 9 additions & 0 deletions week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Worker

def self.work (repeat_count=1)
i = nil
repeat_count.times {i = yield}
i
end

end
19 changes: 18 additions & 1 deletion week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
Please Read:
Please Read:
Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?

Ruby reads files through I/0 -related methods that exist in the Kernel module. THese methods include, gets, open, print, printf, putc, puts, readline and test, which are all used to open and close files, as well as read the files' data and write to the 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?

The Dir class is used as a way or representing directory structures and their contents. This class allows us to manipulate the directory and file structure, search for, rename, delete and create files and directories.


4. What is an IO object?

An IO object is a class that gives a bidirectional channel between a Ruby program and an external resource. The IO object can be written to and read from.

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

Rake is used to build executable programs and libraries from source code. A rake task is an instruction in the form of code that is executed when running rake. A task may be grouped with other tasks, and contain dependencies to other files or tasks.
5 changes: 4 additions & 1 deletion week7/homework/features/step_definitions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# require '../../pirate_translator.rb'
require File.join(File.dirname(__FILE__), '..', '..', 'pirate_translator')

Gangway /^I have a (\w+)$/ do |arg|
@translator = Kernel.const_get(arg).new
end
Expand All @@ -16,4 +19,4 @@

Letgoandhaul /^it also prints '(.+)'$/ do |arg|
@result.split("\n ").last.should == arg
end
end
3 changes: 3 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe-steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'rspec/mocks/standalone'
require 'rspec/expectations'

require File.join(File.dirname(__FILE__), '../', '../', 'tic_tac_toe.rb')

Given /^I start a new Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new
end
Expand Down
10 changes: 10 additions & 0 deletions week7/homework/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class PirateTranslator
def say(statement)
puts statement
end

def translate(statement = '')
"Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"
end

end
5 changes: 3 additions & 2 deletions week7/homework/play_game.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require './features/step_definitions/tic-tac-toe.rb'
#require './features/step_definitions/tic-tac-toe-steps.rb'
require './tic_tac_toe.rb'

@game = TicTacToe.new
puts "What is your name?"
Expand All @@ -10,7 +11,7 @@
when "Computer"
@game.computer_move
when @game.player
@game.indicate_palyer_turn
@game.indicate_player_turn
@game.player_move
end
puts @game.current_state
Expand Down
16 changes: 16 additions & 0 deletions week7/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming

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

method_missing is a method that can be used when and object is sent a message that it cannot find a method for. method_missing will override the defauly error that gets raised, and depending on the code you write within method_missing, handles the exception in a more graceful manner.

2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?

An EignenClass is a metaclass of an object that is used to store class methods and singleton methods. When a Singleton method is created in an object, Ruby creates an Eigenclass class and stores the method there.

Use duck typing when operating on collections containing objects from different classes. Duck typing helps you to reuse code, and reduces the amount of code you have to maintain.

3. When would you use DuckTypeing? How would you use it to improve your code?

You can use ducktyping to accept different data types in your methods so that you have more reusable code, for example a method that can accept either and array or a hash as an argument means you don't have to check type, or write for different data types, resulting in longer, less maintainable code.


4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval?
A Class method will run on the class object itself, wheras an instance method will run on the instance of a class object, and have access to the data of that specific instance.
class_eval allows you to evaluate code on the class object, where instance_eval allows you to evaluate code with an particular instance of a class.

5. What is the difference between a singleton class and a singleton method?
When you add a method to a specific object Ruby inserts a new anonymous class into the inheritance hierarchy as a container to hold these types of methods. A singleton method is a method that is given to only a single object.
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!

Loading