diff --git a/notes/week2_notes.txt b/notes/week2_notes.txt new file mode 100644 index 0000000..46cc214 --- /dev/null +++ b/notes/week2_notes.txt @@ -0,0 +1,75 @@ +"git remote -v" lists all repositories that are connected + +you can pass the git clone any .git directory you want + +git remote add other /Users/jon/test_git + +git remote add class_repo https://github.com/UWE-Ruby/RubyFall2013.git + +git pull origin master + +upstream + +git branch answers + +git checkout answers + +git log --oneline + +----- + +JRuby - converts ruby code into java code + +MRI == CRuby + +respond_to? +what messages does the object respond to? +* method calls or properties + +rather than checking the type, check to see if it +responds to the message you are sending it + +^this is the principle of duck typing + +sudo gem install grb + +simplies working with remote branches + +"Hello world".respond_to? "+" + +.inspect + +%q(hello world) +%Q(hello world) + +ruby inherits smalltalk's thinking in that +it treats methods as messages that are responded to. + +a # sign in front of a method in the docs indicates it is an instance +:: in the docs indicates a class method + +"hello".send("size") +"hello".size + +"hello".send("+", "world") + +my_name.chop! + +.object_id + +my_name = "Hello" +other_name = my_name +my_name = "Hello World" +puts other_name +# prints "Hello" and NOT "Hello World" +# because other_name is pointing to the +# original "Hello" string literal object + + + + + + + + + diff --git a/notes/week3_notes.txt b/notes/week3_notes.txt new file mode 100644 index 0000000..394eaf6 --- /dev/null +++ b/notes/week3_notes.txt @@ -0,0 +1,101 @@ +To implement Enumerable just make a method called each, which +usually calls each on something else... + +@ sign before a variable makes it an instance variable + +looks for a method a called title= when you see myClass.title = "Hello" + +def title= t + @title = t +end + +attr_accessor :property +attr_reader :property +attr_writer :property + +self. before a method name make its a class level method -- like a static method + +class variables are defined with two @@ signs + +@inst_var +@@class_var + +three ways of declaring a class level method: + +myClass.myMethod() <--- you have to change the name of the method when you change the name of the class +self.myMethod() <--you dont have to do the above + +self << + def myMethod() + end + +^^in the above case you are adding a method to the self obj, weird syntax + +::new = class level +#new = instance level + +default values: + +def initialize title = "Not Set" + @title = title +end + +||= is "or equals" + +@@book_count ||= 0 + +"if @@book_count is not set, set to 0, otherwise return @@book_count" + +ruby does not use ++ + +use i += 3 + + +arrays and hashes + +.first and .last + +you can access from the end of the array like myArray[-2] + +.select { } + +.map and .collect + +a.map!{ } + +what does the bang do? it does it in-place (aka works on the original object, not a copy) + +on an array + +h.each do |k, v| end + +hash.keys and h.values + +myArray.include?(1) +myHash.has_key?(:hello => "Hello") + +h[:hello] +h["hello"] + +read about symbols + +.count + +.each vs .map + +.each returns the original array passed to it +.map returns the new + +.inject and .reduce -- mean the same thing + +$monsters.inject(0){ |legs, m| legs += m[:legs] } + +0 is the start value, in this case, i can actually leave the start value out, +if you leave it out it tags the first thing + + +legs is a reference to the running total + +dangers.inject(Hash.new(0)){ |hist, danger| hist[danger] += 1 }. + +semicolon in irb is a pretend line break diff --git a/notes/week4_notes.txt b/notes/week4_notes.txt new file mode 100644 index 0000000..256b6aa --- /dev/null +++ b/notes/week4_notes.txt @@ -0,0 +1,80 @@ +----------------- +notes from week4 +----------------- +require "#{File.dirname(__FILE__)}/worker" +you can say require './named_thing.rb' + +this will require a file called worker in the same directory +as the file that is being run + +# summing numbers with inject +input.inject(0, :+) + +# this will sum +# start with 0 and apply this method to each method and the result + +# multiplying with +splat args -- (*args) + +# you can use flatten to Returns a new array that is a one-dimensional flattening of self (recursively). +# That is, for every element that is an array, extract its elements into the new array. +# The optional level argument determines the level of recursion to flatten. + +def fac n + product = 1 + 1.upto(n){ |i| product *= 1 } + product +end + +recursive functions + +statement modifiers -- see below + +def fac 1 + return 1 if n==0 # one of the only times you are going to call return + n * fac(n-1) +end + +money gem + +BigDecimal class + +to_s and to_sym methods + +.cover? + +(2..200000).cover? 6 + +does this range include the value 6? + +.. is inclusive + +... is inclusive + +1..5 is 1 through 5 and 1...5 is 1 through 4 + +('a'..'z').cover? 'B' => false + +def <==> (other) + @value <==> other.value +end + +The Comparable module + +block_given? --- was there a block given? + +you can pass a bloc kto any method in ruby, whether or not it does anything with it + +passing parameters to block + +yield(paramA, paramB) + +call(x) + +when you use the syntax &myBlock you are turning it into a lambda by giving it a name + +def test hello, world, &my_code + my_code.call +end + +test "bonjour", "world", {puts "hi"} diff --git a/week1/class_materials/.rspec b/week1/class_materials/.rspec new file mode 100644 index 0000000..e24f5e0 --- /dev/null +++ b/week1/class_materials/.rspec @@ -0,0 +1 @@ +--format nested --color diff --git a/week1/class_materials/name_printer_spec.rb b/week1/class_materials/name_printer_spec.rb new file mode 100644 index 0000000..80b8cc2 --- /dev/null +++ b/week1/class_materials/name_printer_spec.rb @@ -0,0 +1,12 @@ +describe "NamePrinter Application" do + + context "When Renee is logged in" do + it "should say Renee" do + "Renee".should eq "Renee" + end + end + + context "Wen Renee is logged out" do + end + +end \ No newline at end of file diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1e0a8ef..58a1ba0 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,11 +43,12 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 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)" do + end it "requires that examples use expectations (like #should) to work properly" do @@ -77,15 +78,23 @@ # 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 -13 + (1+2-5*6/2).should eq -12 end + it "should count the characters in your name" do - pending + "Jonathan".should have(8).characters + "Jon".should_not have(4).characters end - it "should check basic math" + it "should check basic math" do + (3 % 2).should eq 1 + (33 * 3).should eq 99 + end - it "should check basic spelling" + it "should check basic spelling" do + "Jonathan".should_not include "Z" + "Jonathan".should include "Jon" + end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..b2abffd 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,36 @@ 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 representation in memory of a specific concept or thing that the Ruby interpreter knows about. + +- An object is an instance of a class and is an entity that has properties and behaviors; represents data. 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. + +- A variable is a reference to an object (labeled container) 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). + +- A class is a blueprint for an object. The class is the cookie cutter, objects are the cookies. 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[]). + +- An object that holds a collection of characters and provides methods for manipulating these characters. 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 + +- .concat, .upcase, .chomp 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +<<<<<<< HEAD + +- double and single quotes. double quotes allow variable inerpolation e.g. "#{foobar}" +======= +Single quotes ex: '' and Double quotes ex: "". The single quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same. +>>>>>>> 02ccc7d94fdfc30a6d2d7e93c8bee3f07a3da618 diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..e608a39 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,15 +12,28 @@ 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" +<<<<<<< HEAD + + it "should be able to count the charaters" do + @my_string.count("R").should eq 2 + end + +======= + it "should be able to count the characters" do + @my_string.should have(@my_string.size).characters + end +>>>>>>> 02ccc7d94fdfc30a6d2d7e93c8bee3f07a3da618 it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + 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 + + it "should be equal the upper case version" do + @my_string.upcase.should eq "RENéE IS A FUN TEACHER. RUBY IS A REALLY COOL PROGRAMMING LANGUAGE" + end end end - diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..878541d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,52 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +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. + +* Both have keys and values. The keys in an array must be integers and +must be sequential starting at 0. The keys in a hash can be any +object. Hashes are also called associative arrays and/or dictionaries 2. When would you use an Array over a Hash and vice versa? +When the items have an inherent order I would use an array, when I want to reference the items in my collection by a name or key and their order does not matter I would use a hash. + +* Arrays are great for storing things in a particular order; +a hash is great for retrieving things based on something +other than order. If you don't know the position of an +element in an array, but you know the name of an element, +you still have to enumerate through all of the elements to find +a particular element with an array. You can only locate an element by +position with an array. With a hash, you could retrieve the +desired element using the name, instead of the position index. The reason you +can do this is that a hash accepts any object as a key, unlike an array that +only accepts an integer representing an's elements position in the array. 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a way to group code that you can use across multiple classes. Enumerable is a Ruby module that provides collection functionality; iteration, searching, and sorting. It requires an implementation of the each method. + +* A module allows you to organize code into namespaces. It also +allows classes to mixin code rather than inherit code. 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No, multiple inheritance is not allowed in Ruby. You can include multiple modules if you wanted to mix-in different functionality into your code. Code that is related with a hierarchical nature should be subclassed (inherited). A class can only have 1 direct parent, but can have lots of ancestors. + +* No. You use modules. If you could inherit from more than one class you would +have the diamond problem. If a class inherited from two classes that both defined a method named +hello(), which superclass's method would be called if you called hello() on the child class? +Modules allow you to re-use code without inheritance which circumvents the problem +of multiple inheritance. 5. What is the difference between a Module and a Class? +<<<<<<< HEAD + +* "Classes are about objects; modules are about functions." (stole this from StackOverflow) +* You can't inherit from a module +* You can't instantiate a module +* Modules can't have variables, but both modules and classes can have constants and methods +* Modules can add instance methods to a class +* You use modules for mixins and classes for inheritance and/or instantiation +* Modules let you create namespaces +* Modules are like libraries in that you can use across multiple classes +======= +A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. +>>>>>>> 02ccc7d94fdfc30a6d2d7e93c8bee3f07a3da618 diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..22ad66e --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,56 @@ +module SimonSays +<<<<<<< HEAD + def echo(phrase) + phrase + end + + def shout(phrase) + phrase.upcase + end + + def repeat(*args) + if args.length == 1 + "#{args[0]} #{args[0]}" + elsif args.length == 2 + ("#{args[0]} " * args[1]).strip + else + #throw an error for wrong + #number of args + end + end + + def start_of_word(*args) + if args.length == 2 + args[0][0..(args[1]-1)] + else + #throw an error for wrong + #number of args + end + end + + def first_word(phrase) + phrase.split(' ')[0] + end +end +======= + def echo(st) + st + end + + def shout(st) + st.upcase + end + + def first_word(st) + st.split.first + end + + def start_of_word(st,i) + st[0...i] + end + + def repeat(st, t=2) + ([st]*t).join(' ') + end +end +>>>>>>> 02ccc7d94fdfc30a6d2d7e93c8bee3f07a3da618 diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..2c6db49 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,50 @@ +class Calculator + + def sum(arr) +<<<<<<< HEAD + res = 0 + arr.each do |num| + res += num + end + res + end + + def multiply(*args) + if args.length == 1 + args[0][0] * args[0][1] + elsif args.length == 2 + args[0] * args[1] + else + 0 + end + end + + def pow(num, power) + (1..power).each |i| do + end + end + + def fac(n) +======= + arr.inject(0){ |result, number| result + number } + end + + def multiply(arg1, arg2 = 1) + if arg1.respond_to? 'each' + arg1.inject(1){ |result, fac| result *= fac } + else + arg1*arg2 + end + end + + def pow(num, power) + base = num + (power-1).times { num *= base } + num + end + + def fac(n) + (1..n).inject(1){ |result, fac| result * fac } +>>>>>>> 19d4e50a1b0af56a8d430a1d46d13fae32e3668f + end +end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..a858638 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,16 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +an immutable string preceded by a colon : 2. What is the difference between a symbol and a string? +symbols are immutable (cannot be changed once assigned) 3. What is a block and how do I call a block? +a block of code. yield. 4. How do I pass a block to a method? What is the method signature? +def myMethod(&blockOfCode) 5. Where would you use regular expressions? +To match and manipulate text! diff --git a/week4/class_materials/doc/Monster.html b/week4/class_materials/doc/Monster.html new file mode 100644 index 0000000..2cc92e5 --- /dev/null +++ b/week4/class_materials/doc/Monster.html @@ -0,0 +1,267 @@ + + + + + + +class Monster - RDoc Documentation + + + + + + + + + + + + + + + + +
+

class Monster

+ +
+ +
+ + + + +
+ + + + + + + + +
+

Attributes

+ + +
+
+ dangers[RW] +
+ +
+ + + +
+
+ +
+
+ legs[R] +
+ +
+ + + +
+
+ +
+
+ nocturnal[R] +
+ +
+ + + +
+
+ +
+
+ vulnerabilities[RW] +
+ +
+ + + +
+
+ +
+ + + + +
+

Public Class Methods

+ + +
+ +
+ new(noc, legs, name="Monster", vul = [], dangers = []) + + click to toggle source + +
+ + +
+ + + + +
+ Calls superclass method + NamedThing.new +
+ + + +
+
# File monster.rb, line 7
+def initialize(noc, legs, name="Monster", vul = [], dangers = [])
+        super(name)
+        @nocturnal = noc
+        @vlunerabilities = vul
+        @dangers = dangers
+        @legs = legs
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week4/class_materials/doc/NamedThing.html b/week4/class_materials/doc/NamedThing.html new file mode 100644 index 0000000..f17bcb7 --- /dev/null +++ b/week4/class_materials/doc/NamedThing.html @@ -0,0 +1,277 @@ + + + + + + +module NamedThing - RDoc Documentation + + + + + + + + + + + + + + + + +
+

module NamedThing

+ +
+ +
+ + + + +
+ + + + + + + + +
+

Attributes

+ + +
+
+ name[RW] +
+ +
+ + + +
+
+ +
+ + + + +
+

Public Class Methods

+ + +
+ +
+ new(name) + + click to toggle source + +
+ + +
+ + + + + + +
+
# File named_thing.rb, line 4
+def initialize(name)
+        @name = name
+end
+
+ +
+ + + + +
+ + +
+ +
+

Public Instance Methods

+ + +
+ +
+ say_name() + + click to toggle source + +
+ + +
+ + + + + + +
+
# File named_thing.rb, line 8
+def say_name
+        "My name is #{@name}"
+end
+
+ +
+ + + + +
+ + +
+ +
+ shout_name() + + click to toggle source + +
+ + +
+ + + + + + +
+
# File named_thing.rb, line 12
+def shout_name
+        @name.upcase
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week4/class_materials/doc/OddNumber.html b/week4/class_materials/doc/OddNumber.html new file mode 100644 index 0000000..fae3be9 --- /dev/null +++ b/week4/class_materials/doc/OddNumber.html @@ -0,0 +1,289 @@ + + + + + + +class OddNumber - RDoc Documentation + + + + + + + + + + + + + + + + +
+

class OddNumber

+ +
+ +
+ + + + +
+ + + + + + + + +
+

Attributes

+ + +
+
+ value[RW] +
+ +
+ + + +
+
+ +
+ + + + +
+

Public Class Methods

+ + +
+ +
+ new(value) + + click to toggle source + +
+ + +
+ + + + + + +
+
# File odd_number.rb, line 4
+def initialize(value)
+        @value = value
+end
+
+ +
+ + + + +
+ + +
+ +
+

Public Instance Methods

+ + +
+ +
+ <=>(other) + + click to toggle source + +
+ + +
+ + + + + + +
+
# File odd_number.rb, line 18
+def <=> (other)
+        @value <=> other.value
+end
+
+ +
+ + + + +
+ + +
+ +
+ succ() + + click to toggle source + +
+ + +
+ + + + + + +
+
# File odd_number.rb, line 8
+def succ
+        new_val = nil
+        if @value.even?
+                new_val =OddNumber.new(@value + 1)
+        else
+                new_val =OddNumber.new(@value + 2)
+        end
+        new_val
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week4/class_materials/doc/Timer.html b/week4/class_materials/doc/Timer.html new file mode 100644 index 0000000..d3f0971 --- /dev/null +++ b/week4/class_materials/doc/Timer.html @@ -0,0 +1,190 @@ + + + + + + +class Timer - RDoc Documentation + + + + + + + + + + + + + + + + +
+

class Timer

+ +
+ +
+ + + + +
+ + + + + + + + + + +
+

Public Class Methods

+ + +
+ +
+ time_code(n=1) { || ... } + + click to toggle source + +
+ + +
+ + + + + + +
+
# File timer.rb, line 2
+def self.time_code(n=1)
+        start_time = Time.now
+        n.times{yield}
+        end_time = Time.now
+        (end_time - start_time) / n.to_f
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week4/class_materials/doc/Vampire.html b/week4/class_materials/doc/Vampire.html new file mode 100644 index 0000000..7be1ac0 --- /dev/null +++ b/week4/class_materials/doc/Vampire.html @@ -0,0 +1,192 @@ + + + + + + +class Vampire - RDoc Documentation + + + + + + + + + + + + + + + + +
+

class Vampire

+ +
+ +
+ + + + +
+ + + + + + + + + + +
+

Public Class Methods

+ + +
+ +
+ new(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dangers=[:bites]) + + click to toggle source + +
+ + +
+ + + + +
+ Calls superclass method + Monster.new +
+ + + +
+
# File vampire.rb, line 3
+def initialize(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dangers=[:bites])
+        super(noc,legs,name,vul,dangers)
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week4/class_materials/doc/created.rid b/week4/class_materials/doc/created.rid new file mode 100644 index 0000000..60baa2e --- /dev/null +++ b/week4/class_materials/doc/created.rid @@ -0,0 +1,9 @@ +Tue, 29 Oct 2013 20:17:08 -0700 +./doc/created.rid Tue, 29 Oct 2013 20:17:08 -0700 +./monster.rb Tue, 29 Oct 2013 18:52:22 -0700 +./named_thing.rb Tue, 29 Oct 2013 17:56:38 -0700 +./odd_number.rb Tue, 29 Oct 2013 17:56:38 -0700 +./resources.txt Tue, 29 Oct 2013 17:56:38 -0700 +./timer.rb Tue, 29 Oct 2013 20:16:38 -0700 +./timer_spec.rb Tue, 29 Oct 2013 17:56:38 -0700 +./vampire.rb Tue, 29 Oct 2013 17:56:38 -0700 diff --git a/week4/class_materials/doc/doc/created_rid.html b/week4/class_materials/doc/doc/created_rid.html new file mode 100644 index 0000000..f34aa10 --- /dev/null +++ b/week4/class_materials/doc/doc/created_rid.html @@ -0,0 +1,93 @@ + + + + + + +created.rid - RDoc Documentation + + + + + + + + + + + + + + + + +
+ +
+ + + + + diff --git a/week4/class_materials/doc/images/add.png b/week4/class_materials/doc/images/add.png new file mode 100644 index 0000000..6332fef Binary files /dev/null and b/week4/class_materials/doc/images/add.png differ diff --git a/week4/class_materials/doc/images/arrow_up.png b/week4/class_materials/doc/images/arrow_up.png new file mode 100644 index 0000000..1ebb193 Binary files /dev/null and b/week4/class_materials/doc/images/arrow_up.png differ diff --git a/week4/class_materials/doc/images/brick.png b/week4/class_materials/doc/images/brick.png new file mode 100644 index 0000000..7851cf3 Binary files /dev/null and b/week4/class_materials/doc/images/brick.png differ diff --git a/week4/class_materials/doc/images/brick_link.png b/week4/class_materials/doc/images/brick_link.png new file mode 100644 index 0000000..9ebf013 Binary files /dev/null and b/week4/class_materials/doc/images/brick_link.png differ diff --git a/week4/class_materials/doc/images/bug.png b/week4/class_materials/doc/images/bug.png new file mode 100644 index 0000000..2d5fb90 Binary files /dev/null and b/week4/class_materials/doc/images/bug.png differ diff --git a/week4/class_materials/doc/images/bullet_black.png b/week4/class_materials/doc/images/bullet_black.png new file mode 100644 index 0000000..5761970 Binary files /dev/null and b/week4/class_materials/doc/images/bullet_black.png differ diff --git a/week4/class_materials/doc/images/bullet_toggle_minus.png b/week4/class_materials/doc/images/bullet_toggle_minus.png new file mode 100644 index 0000000..b47ce55 Binary files /dev/null and b/week4/class_materials/doc/images/bullet_toggle_minus.png differ diff --git a/week4/class_materials/doc/images/bullet_toggle_plus.png b/week4/class_materials/doc/images/bullet_toggle_plus.png new file mode 100644 index 0000000..9ab4a89 Binary files /dev/null and b/week4/class_materials/doc/images/bullet_toggle_plus.png differ diff --git a/week4/class_materials/doc/images/date.png b/week4/class_materials/doc/images/date.png new file mode 100644 index 0000000..783c833 Binary files /dev/null and b/week4/class_materials/doc/images/date.png differ diff --git a/week4/class_materials/doc/images/delete.png b/week4/class_materials/doc/images/delete.png new file mode 100644 index 0000000..08f2493 Binary files /dev/null and b/week4/class_materials/doc/images/delete.png differ diff --git a/week4/class_materials/doc/images/find.png b/week4/class_materials/doc/images/find.png new file mode 100644 index 0000000..1547479 Binary files /dev/null and b/week4/class_materials/doc/images/find.png differ diff --git a/week4/class_materials/doc/images/loadingAnimation.gif b/week4/class_materials/doc/images/loadingAnimation.gif new file mode 100644 index 0000000..82290f4 Binary files /dev/null and b/week4/class_materials/doc/images/loadingAnimation.gif differ diff --git a/week4/class_materials/doc/images/macFFBgHack.png b/week4/class_materials/doc/images/macFFBgHack.png new file mode 100644 index 0000000..c6473b3 Binary files /dev/null and b/week4/class_materials/doc/images/macFFBgHack.png differ diff --git a/week4/class_materials/doc/images/package.png b/week4/class_materials/doc/images/package.png new file mode 100644 index 0000000..da3c2a2 Binary files /dev/null and b/week4/class_materials/doc/images/package.png differ diff --git a/week4/class_materials/doc/images/page_green.png b/week4/class_materials/doc/images/page_green.png new file mode 100644 index 0000000..de8e003 Binary files /dev/null and b/week4/class_materials/doc/images/page_green.png differ diff --git a/week4/class_materials/doc/images/page_white_text.png b/week4/class_materials/doc/images/page_white_text.png new file mode 100644 index 0000000..813f712 Binary files /dev/null and b/week4/class_materials/doc/images/page_white_text.png differ diff --git a/week4/class_materials/doc/images/page_white_width.png b/week4/class_materials/doc/images/page_white_width.png new file mode 100644 index 0000000..1eb8809 Binary files /dev/null and b/week4/class_materials/doc/images/page_white_width.png differ diff --git a/week4/class_materials/doc/images/plugin.png b/week4/class_materials/doc/images/plugin.png new file mode 100644 index 0000000..6187b15 Binary files /dev/null and b/week4/class_materials/doc/images/plugin.png differ diff --git a/week4/class_materials/doc/images/ruby.png b/week4/class_materials/doc/images/ruby.png new file mode 100644 index 0000000..f763a16 Binary files /dev/null and b/week4/class_materials/doc/images/ruby.png differ diff --git a/week4/class_materials/doc/images/tag_blue.png b/week4/class_materials/doc/images/tag_blue.png new file mode 100644 index 0000000..3f02b5f Binary files /dev/null and b/week4/class_materials/doc/images/tag_blue.png differ diff --git a/week4/class_materials/doc/images/tag_green.png b/week4/class_materials/doc/images/tag_green.png new file mode 100644 index 0000000..83ec984 Binary files /dev/null and b/week4/class_materials/doc/images/tag_green.png differ diff --git a/week4/class_materials/doc/images/transparent.png b/week4/class_materials/doc/images/transparent.png new file mode 100644 index 0000000..d665e17 Binary files /dev/null and b/week4/class_materials/doc/images/transparent.png differ diff --git a/week4/class_materials/doc/images/wrench.png b/week4/class_materials/doc/images/wrench.png new file mode 100644 index 0000000..5c8213f Binary files /dev/null and b/week4/class_materials/doc/images/wrench.png differ diff --git a/week4/class_materials/doc/images/wrench_orange.png b/week4/class_materials/doc/images/wrench_orange.png new file mode 100644 index 0000000..565a933 Binary files /dev/null and b/week4/class_materials/doc/images/wrench_orange.png differ diff --git a/week4/class_materials/doc/images/zoom.png b/week4/class_materials/doc/images/zoom.png new file mode 100644 index 0000000..908612e Binary files /dev/null and b/week4/class_materials/doc/images/zoom.png differ diff --git a/week4/class_materials/doc/index.html b/week4/class_materials/doc/index.html new file mode 100644 index 0000000..890f0da --- /dev/null +++ b/week4/class_materials/doc/index.html @@ -0,0 +1,90 @@ + + + + + + +RDoc Documentation + + + + + + + + + + + + + + + + +
+

This is the API documentation for RDoc Documentation. +

+ + + + diff --git a/week4/class_materials/doc/js/darkfish.js b/week4/class_materials/doc/js/darkfish.js new file mode 100644 index 0000000..f26fd45 --- /dev/null +++ b/week4/class_materials/doc/js/darkfish.js @@ -0,0 +1,155 @@ +/** + * + * Darkfish Page Functions + * $Id: darkfish.js 53 2009-01-07 02:52:03Z deveiant $ + * + * Author: Michael Granger + * + */ + +/* Provide console simulation for firebug-less environments */ +if (!("console" in window) || !("firebug" in console)) { + var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", + "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]; + + window.console = {}; + for (var i = 0; i < names.length; ++i) + window.console[names[i]] = function() {}; +}; + + +/** + * Unwrap the first element that matches the given @expr@ from the targets and return them. + */ +$.fn.unwrap = function( expr ) { + return this.each( function() { + $(this).parents( expr ).eq( 0 ).after( this ).remove(); + }); +}; + + +function showSource( e ) { + var target = e.target; + var codeSections = $(target). + parents('.method-detail'). + find('.method-source-code'); + + $(target). + parents('.method-detail'). + find('.method-source-code'). + slideToggle(); +}; + +function hookSourceViews() { + $('.method-heading').click( showSource ); +}; + +function toggleDebuggingSection() { + $('.debugging-section').slideToggle(); +}; + +function hookDebuggingToggle() { + $('#debugging-toggle img').click( toggleDebuggingSection ); +}; + +function hookTableOfContentsToggle() { + $('.indexpage li .toc-toggle').each( function() { + $(this).click( function() { + $(this).toggleClass('open'); + }); + + var section = $(this).next(); + + $(this).click( function() { + section.slideToggle(); + }); + }); +} + +function hookSearch() { + var input = $('#search-field').eq(0); + var result = $('#search-results').eq(0); + $(result).show(); + + var search_section = $('#search-section').get(0); + $(search_section).show(); + + var search = new Search(search_data, input, result); + + search.renderItem = function(result) { + var li = document.createElement('li'); + var html = ''; + + // TODO add relative path to + + + + + + + + + + + + +
+ +

Blocks, Procs, Lambdas, and Closures: www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

+ +
+ + + + + diff --git a/week4/class_materials/doc/table_of_contents.html b/week4/class_materials/doc/table_of_contents.html new file mode 100644 index 0000000..29f1ec6 --- /dev/null +++ b/week4/class_materials/doc/table_of_contents.html @@ -0,0 +1,86 @@ + + + + + + +Table of Contents - RDoc Documentation + + + + + + + + + + + + + + +

Table of Contents - RDoc Documentation

+ +

Pages

+ + +

Classes/Modules

+ + +

Methods

+ + + + + diff --git a/week4/class_materials/monster.rb b/week4/class_materials/monster.rb index 013c3d2..bbe1bf3 100644 --- a/week4/class_materials/monster.rb +++ b/week4/class_materials/monster.rb @@ -11,4 +11,4 @@ def initialize(noc, legs, name="Monster", vul = [], dangers = []) @dangers = dangers @legs = legs end -end +end diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb index 67b7681..ae7abb2 100644 --- a/week4/class_materials/timer.rb +++ b/week4/class_materials/timer.rb @@ -5,4 +5,5 @@ def self.time_code(n=1) end_time = Time.now (end_time - start_time) / n.to_f end -end \ No newline at end of file +end + diff --git a/week4/exercises/worker_spec.rb b/week4/exercises/worker_spec.rb index dfc6f02..b26ec7d 100644 --- a/week4/exercises/worker_spec.rb +++ b/week4/exercises/worker_spec.rb @@ -1,5 +1,4 @@ require "#{File.dirname(__FILE__)}/worker" - describe Worker do it "executes a block and returns a string" do @@ -24,7 +23,6 @@ result.should == 5 end - it "executes a block 3 times and returns the result" do n = 5 result = Worker.work(3) do @@ -34,3 +32,4 @@ end end + diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..caa4e88 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,19 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +With the class-level method "open" in the File class (e.g. File.open) + 2. How would you output "Hello World!" to a file called my_output.txt? +With the instance-level method "write" in a File object (e.g. f.write) + 3. What is the Directory class and what is it used for? +Represents a directory (folder). You can use this to view and manipulate +files and sub-directories inside a directory on the filesystem. + 4. What is an IO object? +IO is the superclass (parent) of File. It is the basis for all input and output. + 5. What is rake and what is it used for? What is a rake task? +Rake is Ruby's version of make. You use rake to build projects, +manage dependencies, run test suites, etc. A rake task is a set of actions. +A task can be dependent upon another task. diff --git a/week7/homework/features/pirate.feature b/week7/homework/features/pirate.feature deleted file mode 100644 index 7de1a0b..0000000 --- a/week7/homework/features/pirate.feature +++ /dev/null @@ -1,11 +0,0 @@ -# language: en-pirate - -Ahoy matey!: Pirate Speak - I would like help to talk like a pirate - -Heave to: The mighty speaking pirate - Gangway! I have a PirateTranslator - Blimey! I say 'Hello Friend' - Aye I hit translate - Let go and haul it prints out 'Ahoy Matey' - Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..84801bf 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,14 @@ +class PirateTranslator + attr_accessor :phrase + def say(p='') + @phrase = p + end + + def translate + "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end +end + 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..d1c6002 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -21,7 +21,7 @@ end Given /^I have a started Tic\-Tac\-Toe game$/ do - @game = TicTacToe.new(:player) + @game = TicTacToe.new() @game.player = "Renee" end @@ -67,14 +67,15 @@ @game.player_symbol.should eq :X end + + When /^I enter a position "(.*?)" on the board$/ do |arg1| - @old_pos = @game.board[arg1.to_sym] - @game.should_receive(:get_player_move).and_return(arg1) - @game.player_move.should eq arg1.to_sym + # @game.player_move(arg1.to_sym) end When /^"(.*?)" is not taken$/ do |arg1| - @old_pos.should eq " " + @game.board[arg1.to_sym].should eq " " + @game.player_move(arg1.to_sym) end Then /^it is now the computer's turn$/ do @@ -118,7 +119,5 @@ end Then /^computer should ask me for another position "(.*?)"$/ do |arg1| - @game.board[arg1.to_sym] = ' ' - @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) - @game.player_move.should eq arg1.to_sym + @game.player_move(arg1.to_sym) end diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..a0d0e47 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,220 @@ +class TicTacToe + attr_accessor :current_player + attr_accessor :player_counter + attr_accessor :board + attr_accessor :computer_symbol + attr_accessor :player_symbol + attr_accessor :winning_symbol + attr_accessor :winner + + SYMBOLS = [:X, :O] + + def initialize(first = nil, symbol = nil) + if (first.nil? || !([:player, :computer].include? first)) + # Decide who goes first randomly + @current_player = @player + # Randomly assign symbols + @computer_symbol = SYMBOLS.sample + @player_symbol = :O if @computer_symbol == :X + @player_symbol = :X if @computer_symbol == :O + else + @current_player = @player if first == :player + @current_player = "Computer" if first == :computer + if (symbol.nil? || !(SYMBOLS.include? symbol)) + # Randomly assign symbols + @computer_symbol = SYMBOLS.sample + @player_symbol = :O if @computer_symbol == :X + @player_symbol = :X if @computer_symbol == :O + else + @player_symbol = symbol + @computer_symbol = :O if @player_symbol == :X + @computer_symbol = :X if @player_symbol == :O + end + end + + # Keep count of the number of plays + @play_counter = 0 + + @winning_symbol = "No Winner" + @board = { + :A1 => ' ', + :B1 => ' ', + :C1 => ' ', + :A2 => ' ', + :B2 => ' ', + :C2 => ' ', + :A3 => ' ', + :B3 => ' ', + :C3 => ' ' + } + end + + def player=(name) + @player = name + @current_player = @player if @current_player != "Computer" + end + + def player + @player + end + + def open_spots + open_spots = [] + @board.keys.each do |cell| + open_spots.push cell if @board[cell] == ' ' + end + open_spots + end + + def spots_open? + return true if open_spots.count > 0 + false + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + # current_state if @play_counter == 0 + end + + def current_state + state = "\n****************\n" + state += @board[:A1].to_s + '|' + @board[:A2].to_s + '|' + @board[:A3].to_s + state += "\n_____\n" + state += @board[:B1].to_s + '|' + @board[:B2].to_s + '|' + @board[:B3].to_s + state += "\n_____\n" + state += @board[:C1].to_s + '|' + @board[:C2].to_s + '|' + @board[:C3].to_s + state += "\n****************\n" + puts state + state + end + + def draw? + false + true if @winner == 'Draw' + end + + def player_won? + false + true if @winner == @player + end + + def computer_won? + false + true if @winner == "Computer" + end + + def computer_move + letter = ('A'..'C').to_a.sample + number = (1..3).to_a.sample + if (@board[(letter + number.to_s).to_sym] == ' ') + @board[(letter + number.to_s).to_sym] = @computer_symbol.to_s + puts "Computer plays #{letter + number.to_s}!" + @current_player = @player + @play_counter += 1 + (letter + number.to_s).to_sym + else + computer_move + end + end + + def player_move(m = nil) + print "? " + if m.nil? + move = gets.chomp + else + move = m + end + if (@board[move.to_sym] == ' ') + @board[move.to_sym] = @player_symbol + puts "#{@player}'s Move:" + @current_player = "Computer" + @play_counter += 1 + @player_move = move + move.to_sym + else + player_move + end + end + + def move + if @current_player == player + player_move + else + computer_move + end + end + + def get_player_move() + @player_move + end + + def welcome_player + "Welcome #{@player}" + end + + def horizontal_win? + ('A'..'C').each do |letter| + if SYMBOLS.include? @board["#{letter}1".to_sym] + if ((@board["#{letter}1".to_sym] == @board["#{letter}2".to_sym]) && (@board["#{letter}2".to_sym] == @board["#{letter}3".to_sym])) + @winning_symbol = @board["#{letter}1".to_sym] + return true + end + end + end + false + end + + def vertical_win? + (1..3).each do |number| + if SYMBOLS.include? @board["A#{number}".to_sym] + if ((@board["A#{number}".to_sym] == @board["B#{number}".to_sym]) && (@board["B#{number}".to_sym] == @board["C#{number}".to_sym])) + @winning_symbol = @board["A#{number}".to_sym] + return true + end + end + end + false + end + + def diagonal_win? + if SYMBOLS.include? @board[:A1] + if ((@board[:A1] == @board[:B2]) && (@board[:B2] == @board[:C3])) + @winning_symbol = @board[:A1] + return true + end + end + + if SYMBOLS.include? @board[:A3] + if ((@board[:A3] == @board[:B2]) && (@board[:B2] == @board[:C1])) + @winning_symbol = @board[:A3] + return true + end + end + end + + def no_plays_left? + ('A'..'C').each do |letter| + (1..3).each do |number| + return false if @board["#{letter}#{number}".to_sym] == ' ' + end + end + true + end + + def over? + false + return true if horizontal_win? + return true if vertical_win? + return true if diagonal_win? + @winning_symbol = 'Draw' if no_plays_left? + return true if no_plays_left? + end + + def determine_winner + if over? + @winner = @player if @winning_symbol == @player_symbol + @winner = "Computer" if @winning_symbol == @computer_symbol + @winner = "Draw" if @winning_symbol == "Draw" + end + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..04a6988 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,46 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +Ruby tries to lookup methods along the inheritance chain by checking +each ancestor of an object to see if it contains a definition for the +method called. You can define a method called method_missing on any object +that will be called if there is no matching definition for the called +method. You can use this like Rails' find_by_* methods which use the method_missing +method to perform call other functions if the method name begins with "find_by_". For instance, +if I call "find_by_first_name" the missing_method function would call something like +find(first_name). In this way, you can respond to methods you haven't even defined, since +the method_missing is called when there is no matching definition and the method_missing function has a +reference to the method name and arguments that are being looked up in the inheritance chain when it was called. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +Singleton methods are defined at the object, not the class, level. For instance, +if I have a class called "Pizza" and two instances of Pizza called "pepperoni" and "mushroom", I +could define a Singleton method by defining a method called "spicy?" on the pepperoni instance, +not the Pizza class. If I try to call mushroom.spicy? Ruby would not find the method +definition, but pepperoni.spicy? would work. Ruby does this by replacing the Pizza class object +in the inheritance chain with an Eigenclass, which is an anonymous class that contains all of +the code from Pizza plus the new method spicy? . When I define a new method on the pepperoni instance, +the pepperoni instance stops inheriting from the Pizza class object and starts inheriting from a new +class that is made specially for the pepperoni object. + 3. When would you use DuckTypeing? How would you use it to improve your code? +Duck typing means types don't matter. What matters is whether the object responds to a message. How that +message is responded to doesn't really matter either. Polymorphism is awesome, but it means that the objects +must share a common inheritance. Duck typing allows you to pass in any object as long as it responds_to? +the message you are sending. For instance, rather than checking the type of an object returned by a method +you can simply check whether the object returned by the method responds_to? the messages you will be sending +and only require the object to meet the minimum requirements. The method can then be changed to pass back +whatever (something that inherits from a totally different class, for instance) and as long as the object +continues to respond to the messages that are sent to it, its all good. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +You call instance methods on the instance object, and you call class methods on the class object. +You can pass a string or block to the class_eval method on a Class instance and it will add to the class's definition. +You can pass a string or block to an object's instance_eval method and it will add it to the object's definition (not shared by other objects from with the same class) +If you pass a string or block to an object's instance_eval method and the object happens to be a Class, you are essentially creating a class method (like class_eval) + + 5. What is the difference between a singleton class and a singleton method? +Singleton classes only allow once instance to be instantiated. This automatically happens +when you define a singleton method (a method on the object itself, not the class). In question 2, +pepperoni is a singleton class single it has the singleton method spicy? \ No newline at end of file diff --git a/week8/class_materials/week8.key b/week8/class_materials/week8.key index 0d82bd6..a50314c 100644 Binary files a/week8/class_materials/week8.key and b/week8/class_materials/week8.key differ