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

Robert's Week 7 Homework #98

Open
wants to merge 20 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
46 changes: 24 additions & 22 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# encoding: utf-8

describe "The Rspec ruby gem" do
context "Domain Specific Language" do

context "Domain Specific Language" do

it "creates examples with the #it keyword" do

Expand Down Expand Up @@ -43,11 +43,12 @@

# When this example fails,
# it will show "expected" as 2, and "actual" as 1
1.should eq 2
# 1.should eq 2
1.should_not eq 2

end

it "supports placeholder examples that lack code (like this one)"
# it "supports placeholder examples that lack code (like this one)"

it "requires that examples use expectations (like #should) to work properly" do

Expand All @@ -67,29 +68,30 @@
end

it "should check how to spell my name" do
"Renée".should include("ée")
"Renée".should include("ée")
end

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 check basic math" do
(40+2).should eq 42
end

it "should check basic spelling" do
"Field".should include('ie')
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
(1+2-3*6/2).should eq -6
end
it "should count the characters in your name" do
"Robert".should have(6).characters
end

it "should check basic math" do
(100-50).should eq 50
end

it "should check basic spelling" do
"Jon".should eq "Jon"
end

end

Expand Down
12 changes: 12 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
An object is a collection of methods and data.
The object responds to messages that are sent to it by defining methods methods.
Objects can be created from a Class using a constructor.


2. What is a variable?
A reference to an object. (Since everything is an object here.)
There are instance variables within objects (@x or @y) which persist in the object. Then there are local variables without a funny prefix. There are $GLOBAL_VARIABLES with a $ prefix. There are variables with two @@ that are associated with the class. Even a class name is a variable, I suppose. They should be capitalized (Klass).

3. What is the difference between an object and a class?
An object is an instance of a class. (But a Class is also an Object, technically.) a class is for creating new objects.

4. What is a String?
An object or, I guess Class that creates objects, which hold text, and has handy methods for operating on text.

5. What are three messages that I can send to a string object? Hint: think methods
"encoding", "length", "+" (does that count?)

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
With double and single quotes.
'I\'m a string.'
"I'm a string...\n with a variable in it. #{x}"
14 changes: 8 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,16 @@
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).characters
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('.') #do something with @my_string here
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"))'
# pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
@my_string.encoding.should eq (Encoding::UTF_8)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions week7/homework/features/step_definitions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require './pirate_translator'

Gangway /^I have a (\w+)$/ do |arg|
@translator = Kernel.const_get(arg).new
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rspec/mocks/standalone'
require 'rspec/expectations'
require './tic_tac_toe'
Given /^I start a new Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new
end
Expand Down Expand Up @@ -35,7 +36,7 @@

Then /^the computer prints "(.*?)"$/ do |arg1|
@game.should_receive(:puts).with(arg1)
@game.indicate_palyer_turn
@game.indicate_player_turn
end

Then /^waits for my input of "(.*?)"$/ do |arg1|
Expand Down
14 changes: 14 additions & 0 deletions week7/homework/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

class PirateTranslator
TRANS = {
'Hello Friend' => 'Ahoy Matey'
}
def translate
TRANS[@word] + "\n " + 'Shiber Me Timbers You Scurvey Dogs!!'
end

def say word
@word = word
end

end
11 changes: 11 additions & 0 deletions week7/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming

Questions:
1. What is method_missing and how can it be used?
It is called by Ruby if it cannot find a method in the inheritance chain. If you want to create methods on the fly, you can use it to catch methods that you have not defined, but wish you did. For example, User.find_by_name_and_phone name, phone could be made up on the fly by the User class to search the in the way you want it to.

2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?
An Eigenclass is an anonomous class just below the class in question in the inheritance chain. Singleton methods live on that Eigenclass/singleton class, which is created when you define Singleton methods.

3. When would you use DuckTypeing? How would you use it to improve your code?
Instead of testing for a type, you just check for behavior, such as does it have X method? Then you can use X method! So if it has the << method to append, just use it. It doesnt matter if the object is an Array or a String as long as you can append to it. You just need to keep track of those things yourself.

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 is on the Class object (actually a singleton class of Class created when you use the class keyword), and an instance method is on the object created by the constructor.
So Cat.meow() will not work, but Cat.new.meow() will. Cat.legs == 4 will return true. This assuming that meow is a method for instances and legs is the general number related to the class of animal, excepting cats with missing legs.
The difference between instance_eval and class_eval is what self is set to. In the former, self is set to the object, and the block will be called as though it were an instance method. If it is called on an ordinary object, this will just be like a method call. If it is called on a Class, then you will be definining class methods if you use the def keyword. In the latter, its set to the Class and things are just as they are when you are defining a class, so you will be defining instance methods if you def anything.

5. What is the difference between a singleton class and a singleton method?
A singleton method is a method slapped onto an object. A singleton class/Eigenclass is the class that ruby creates in order to accommodate your singleton method.