diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1152c22..16f3f11 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..d4fd1c3 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -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}" diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..8f6227b 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -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) @@ -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 diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..acc5576 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,5 @@ +require './pirate_translator' + Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new end diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..c3d52fa 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -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 @@ -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| diff --git a/week7/homework/pirate_translator.rb b/week7/homework/pirate_translator.rb new file mode 100644 index 0000000..4e556cc --- /dev/null +++ b/week7/homework/pirate_translator.rb @@ -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 diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..df8c259 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -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.