diff --git a/midterm/RubyFall2013-midterm b/midterm/RubyFall2013-midterm new file mode 160000 index 0000000..0877e78 --- /dev/null +++ b/midterm/RubyFall2013-midterm @@ -0,0 +1 @@ +Subproject commit 0877e78c8ceb871dbaa4149f5aeb3c4fa9522197 diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index 177358a..7d742a7 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -11,12 +11,125 @@ Instructions for Mid-Term submission and Git Review (10pts): Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? + + - Hash definition + - To denote a block of code as with the do/end + - Interpolation that allows Ruby code to appear within a string: #{1+2} + - What is a regular expression and what is a common use for them? + + - Regular expressions are a powerful tool for working with text, specifically strings. + Though the Ruby String class is quite expansive with more than 100 methods for manipulating strings, + for those situations that the String class can't handle, the power of regular expressions can be + utilized. + - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? + + The basic difference is that Symbols are immutable and Strings are not. Mutable objects, like strings, can + be changed after assignment while immutable objects can only be overwritten. As such, Strings are Ruby + objects having a unique id and and their own place in memory. Symbol usage shares the same object id, and as + such the same space on the memory heap. Every time a a string is created a new object is created, where as, a symbol is + is simply reused until the program operation has completed. As Symbols stay in memory throughout the program's + operation, it can be quickly accessed from memory instead of instantiating a new object for a string. + + This impacts the overhead of Garbage collection as there is one symbol marked for clean up where as every sting object is marked. + + Symbols are not just faster then Strings in how they are stored and used, but also in how they are compared. + As Symbols of the same text share the same space on the heap, testing for equality is only a matter of comparing + the object id’s. Testing equality for Strings is more extensive as every String's actual data must be + compared by the interpreter. + + Fixnum:A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). + If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum. + Fixnum objects have immediate value meaning that when they are assigned or passed as parameters, t + he actual object is passed, rather than a reference to that object. + Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance + for any given integer value, + + Float: + + I remember my CompSci professor saying never to use floats for currency. + + The reason for that is how the IEEE specification defines floats in binary format. Basically, it + stores sign, fraction and exponent to represent a Float. It's like a scientific notation for binary + (something like +1.43*10^2). Because of that, it is impossible to store fractions and decimals in + Float exactly. That's why there is a Decimal format. If you do this: + + irb:001:0> "%.47f" % (1.0/10) + => "0.10000000000000000555111512312578270211815834045" # not "0.1"! + whereas if you just do + + irb:002:0> (1.0/10).to_s + => "0.1" # the interpreter rounds the number for you + So if you are dealing with small fractions, like compounding interests, or maybe even geolocation, + I would highly recommend Decimal format, since in decimal format 1.0/10 is exactly 0.1. + + However, it should be noted that despite being less accurate, floats are processed faster. + Here's a benchmark: + + require "benchmark" + require "bigdecimal" + + d = BigDecimal.new(3) + f = Float(3) + + time_decimal = Benchmark.measure{ (1..10000000).each { |i| d * d } } + time_float = Benchmark.measure{ (1..10000000).each { |i| f * f } } + + puts time_decimal + #=> 6.770960 seconds + puts time_float + #=> 0.988070 seconds + + Basically: + Use float when you don't care about precision too much. For example, some scientific simulations + and calculations only need up to 3 or 4 significant digits. This is useful in trading off accuracy + for speed. Since they don't need precision as much as speed, they would use float. + + Use decimal if you are dealing with numbers that need to be precise and sum up to correct number + (like compounding interests and money-related things). Remember: if you need precision, then you + should always use decimal. + + So if I understand correctly, float is in base-2 whereas decimal is in base-10? What would be a + good use for float? What does your example do, and demonstrate? + no. both of them are base 2. The difference is the structure. My example demonstrates that + 1.0/10 is 0.1 when represented by decimal data structure and 0.100000000000000005 + when represented by float data structure. So if you are working with numbers that need high + precision than using Float will be a mistake. + + When should one use Float, and when Decimal? + + use float when you need speed and dont care about precision too much. for example some + scientific simulations and calculations only care up to 3 or 4 significant digits. + Since they dont need precision as much as speed, you would use float. but if you are dealing + with numbers that need to be precise and sum up to correct number (like compunding interests + and money related things), then you should always use decimal. + + + - Are these two statements equivalent? Why or Why Not? - 1. x, y = "hello", "hello" - 2. x = y = "hello" + 1. x, y = "hello", "hello" - This creates an array: + irb > x,y = "hello" => ["hello","hello"]. + irb > x => "hello" + irb > y => "hello" + 2. x = y = "hello"- This assignment creates one String object: + irb > x = y = "hello" => "hello" + irb > x => "hello" (same as example 1 for x) + irb > y => "hello" (same as example 1 for y) + The difference is the #2 stores one String object. #1 stores an array causing more processing + overhead. + + - What is the difference between a Range and an Array? + An array is an object that's a collection of arbitrary elements. A range is an object that has + a "start" and an "end", and knows how to move from the start to the end without having to + enumerate all the elements in between. + + + + + + - Why would I use a Hash instead of an Array? - What is your favorite thing about Ruby so far? - What is your least favorite thing about Ruby so far? diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt deleted file mode 100644 index bd581a6..0000000 --- a/week1/homework/questions.txt +++ /dev/null @@ -1,15 +0,0 @@ -Please read: -Chapter 3 Classes, Objects, and Variables -p.86-90 Strings (Strings section in Chapter 6 Standard Types) - -1. What is an object? - -2. What is a variable? - -3. What is the difference between an object and a class? - -4. What is a String? - -5. What are three messages that I can send to a string object? Hint: think methods - -6. What are two ways of defining a String literal? Bonus: What is the difference between the two? diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb deleted file mode 100644 index ea79e4c..0000000 --- a/week1/homework/strings_and_rspec_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# encoding: utf-8 - -# 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 -# (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) - -describe String do - context "When a string is defined" do - 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 - 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"))' - end - end -end - diff --git a/week2/.DS_Store b/week2/.DS_Store index 6f2aa37..21c900c 100644 Binary files a/week2/.DS_Store and b/week2/.DS_Store differ diff --git a/week2/exercises/.rspec b/week2/exercises/.rspec new file mode 100644 index 0000000..e061e30 --- /dev/null +++ b/week2/exercises/.rspec @@ -0,0 +1,2 @@ +--format nested +--color \ No newline at end of file diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb deleted file mode 100644 index a6b943d..0000000 --- a/week2/exercises/book.rb +++ /dev/null @@ -1,27 +0,0 @@ -class Book - attr_accessor :title - attr_reader :page_count - - @@book_count = 0 - - def self.book_count - @@book_count - end - - def initialize title = "Not Set", page_count = 0 - @@book_count += 1 - @page_count = page_count - @title = title - end - - - def test - @test = "Hello!" - end - - def out_put_test - puts @test - puts @@book_count - end - -end \ No newline at end of file diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb deleted file mode 100644 index c3b1292..0000000 --- a/week2/exercises/book_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require './book' - -describe Book do - - - context "::book_count" do - - it "should count how many books have been created" do - Book.new - Book.new - Book.new - Book.book_count.should eq 3 - end - - end - - context "::new" do - - it "should set some defaults" do - Book.new.title.should eq "Not Set" - end - - it "should allow us to set the page count" do - book = Book.new "Harry Potter", 5 - book.page_count.should eq 5 - end - - end - - context "#title" do - - before :each do - @book = Book.new - end - - it "should have a title" do - @book.should respond_to "title" - end - - it "should allow me to set the title" do - @book.title = "Snow Crash" - @book.title.should eq "Snow Crash" - end - - - - end - -end \ No newline at end of file diff --git a/week2/homework/.rspec b/week2/homework/.rspec new file mode 100644 index 0000000..e061e30 --- /dev/null +++ b/week2/homework/.rspec @@ -0,0 +1,2 @@ +--format nested +--color \ No newline at end of file diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt deleted file mode 100644 index 939e42d..0000000 --- a/week2/homework/questions.txt +++ /dev/null @@ -1,13 +0,0 @@ -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? - -2. When would you use an Array over a Hash and vice versa? - -3. What is a module? Enumerable is a built in Ruby module, what is it? - -4. Can you inherit more than one thing in Ruby? How could you get around this problem? - -5. What is the difference between a Module and a Class? diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..34df327 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -5,6 +5,10 @@ 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. + ## module SimonSays + ## def echo(st) + ## st + ## end it "should echo hello" do echo("hello").should == "hello" end @@ -12,7 +16,10 @@ it "should echo bye" do echo("bye").should == "bye" end - + ##def shout(st) + ## st.upcase + ##end + it "should shout hello" do shout("hello").should == "HELLO" end @@ -20,6 +27,9 @@ it "should shout multiple words" do shout("hello world").should == "HELLO WORLD" end + ## def repeat(st, t=2) + ## ([st]*t).join(' ') + ## end it "should repeat" do repeat("hello").should == "hello hello" @@ -28,7 +38,9 @@ it "should repeat a number of times" do repeat("hello", 3).should == "hello hello hello" end - + ## def start_of_word(st,i) + ## st[0...i] + ## end it "should return the first letter" do start_of_word("hello", 1).should == "h" end @@ -36,6 +48,9 @@ it "should return the first two letters" do start_of_word("Bob", 2).should == "Bo" end + ## def first_word(st) + ## st.split.first + ## end it "should tell us the first word of 'Hello World' is 'Hello'" do first_word("Hello World").should == "Hello" @@ -45,3 +60,24 @@ first_word("oh dear").should == "oh" end end +##module SimonSays +## 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 diff --git a/week2/mad_libs.rb b/week2/mad_libs.rb new file mode 100644 index 0000000..2103f7d --- /dev/null +++ b/week2/mad_libs.rb @@ -0,0 +1,5 @@ +puts "Please give me a noun" +noun = gets.chomp! # or chomp! +puts "please give me a past tense verb:" +verb = gets.chomp # or chomp! +puts "The #{noun} #{verb} to the store" \ No newline at end of file diff --git a/week3/homework/.rspec b/week3/homework/.rspec new file mode 100644 index 0000000..e061e30 --- /dev/null +++ b/week3/homework/.rspec @@ -0,0 +1,2 @@ +--format nested +--color \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..35e00c1 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,43 @@ +class Calculator + def sum arr + @sum = 0 + arr.each{|n| @sum += n } + return @sum + end + + def multiply *args + @product = 1 + if args.length == 1 + @arr = args[0] + @arr.each do |m| + @product *= m + end + end + if args.length > 1 + @mcand = args[0] + @mplier = args[1] + @arr = [@mcand,@mplier] + @arr.each {|p| @product *= p} + end + return @product + end + def pow *args + p = args[0]**args[1] + return p + end + ##def pow a,b + ## p=a**b + ##end + def fac *args + #The value of 0! is 1, according to the convention for an empty product. + if args[0] == 0 + n = 1 + r = (1..n).reduce(:*) + end + if args[0] != 0 + n = args[0] + r = (1..args[0]).reduce(:*) + end + return r + end +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..072ae7a 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -8,6 +8,7 @@ describe "#sum" do it "computes the sum of an empty array" do + Calculator.new @calculator.sum([]).should == 0 end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..1d235f2 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,41 @@ Please Read: 1. What is a symbol? +A symbol in Ruby is an identifier corresponding to a set of characters often as a name for something. It is +constructed in Ruby by preceding the name with a colon. A particular name will always generate the same symbol, regardless of how +that name is used within the program. The %s delimited notation can also be used to create a symbol. + 2. What is the difference between a symbol and a string? +The basic difference is that Symbols are immutable and Strings are not. Mutable objects, like strings, can +be changed after assignment while immutable objects can only be overwritten. As such, Strings are Ruby +objects having a unique id and and their own place in memory. Symbol usage shares the same object id, and as +such the same space on the memory heap. Every time a a string is created a new object is created, where as, a symbol is +is simply reused until the program operation has completed. As Symbols stay in memory throughout the program's +operation, it can be quickly accessed from memory instead of instantiating a new object for a string. + +This impacts the overhead of Garbage collection as there is one symbol marked for clean up where as every sting object is marked. + +Symbols are not just faster then Strings in how they are stored and used, but also in how they are compared. +As Symbols of the same text share the same space on the heap, testing for equality is only a matter of comparing +the object id’s. Testing equality for Strings is more extensive as every String's actual data must be +compared by the interpreter. 3. What is a block and how do I call a block? +A block is ruby code that is enclosed in {} or do/end notation and is called a 'closure' because +of it's start/end delineation. A method calls a block by using the Ruby yield statement. + +If it is required to reuse a block of code multiple times or if the method using a block +requires a callback, a proc would be recommended over the block. 4. How do I pass a block to a method? What is the method signature? +When calling the method, a block is included in that method call. The '&block' notation is used in the method signature to assign a +block to that variable in the method. + 5. Where would you use regular expressions? +Regular expressions are a powerful tool for working with text, specifically strings. +Though the Ruby String class is quite expansive with more than 100 methods for manipulating strings, +for those situations that the String class can't handle, the power of regular expressions can be +utilized. + + diff --git a/week4/class_materials/week4.key b/week4/class_materials/week4.key index 10d5b17..89b3bef 100644 Binary files a/week4/class_materials/week4.key and b/week4/class_materials/week4.key differ diff --git a/week4/exercises/.rspec b/week4/exercises/.rspec new file mode 100644 index 0000000..e061e30 --- /dev/null +++ b/week4/exercises/.rspec @@ -0,0 +1,2 @@ +--format nested +--color \ No newline at end of file diff --git a/week4/exercises/worker_spec.rb b/week4/exercises/worker_spec.rb index dfc6f02..47deddb 100644 --- a/week4/exercises/worker_spec.rb +++ b/week4/exercises/worker_spec.rb @@ -1,4 +1,5 @@ -require "#{File.dirname(__FILE__)}/worker" +#require "#{File.dirname(__FILE__)}/worker" +require File.join(File.dirname(__FILE__), '..', '/homework/worker') describe Worker do diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..80226e3 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,40 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby defines a single base class, IO, to handle input and output. The same +methods we use for 'simple' i/o from irb, puts, gets, are available for all file +objects: + while line = gets + puts line + end +There are I/O iterators for reading. I/O#each_byte invokes a block with next 8-bit +byte from the IO object. String#dump can also be used + File.open("tesfile") do |file| + file.each_line {|line| puts "got #{line.dump}"} + end + 2. How would you output "Hello World!" to a file called my_output.txt? + + File.open("output.txt", "w") do |file| + file.puts "Hello World!" + end + 3. What is the Directory class and what is it used for? + +Objects of class Dir are directory streams representing directories in the +underlying file system. They provide a variety of ways to list directories and +their contents. + 4. What is an IO object? + +Ruby defines a single base class, IO, to handle input and output. An IO object is +a bidirectional channel between a Ruby program and some external resource. Bidirectional +as in read/write, puts/gets. + + 5. What is rake and what is it used for? What is a rake task? +Rake is a simple ruby build program with capabilities similar to 'make.' Tasks or jobs can be written in ruby +and executed via the ruby Rake command. Files containg ruby code can also be part of the execution. In rails, +rake db:migrate is used to run a database migration script that executes ruby code to build tables according +to the database and table specifications. To view the 'routes' that a ruby application may be configured to run, +the 'rake routes' command will display what the ruby program or application will respond to. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..051d1aa --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,17 @@ +class Worker + def self.work (number = 1) + result = '' + number.times {result = yield if block_given?} + result + end + + #or + + ##class << self + ## def work number=1 + ## result = '' + ## number.times {result = yield if block_given?} + ## result + ## end + ##end +end diff --git a/week7/exercises/features/converter.rb b/week7/exercises/features/converter.rb new file mode 100644 index 0000000..47e30f4 --- /dev/null +++ b/week7/exercises/features/converter.rb @@ -0,0 +1,9 @@ +class Converter + attr_writer :type + def initialize celcius_value + @value = celcius_value.to_f + end + def convert + ((@value -32.0)*(5.0/9.0)).round(1) + end +end \ No newline at end of file diff --git a/week7/exercises/features/converter_steps.rb b/week7/exercises/features/converter_steps.rb new file mode 100644 index 0000000..0029e09 --- /dev/null +++ b/week7/exercises/features/converter_steps.rb @@ -0,0 +1,53 @@ +Given(/^I have entered (\d+) into the converter$/) do |arg1| + #pending # express the regexp above with the code you wish you had + @converter = Converter.new(arg1) +end + +Given(/^I set the type to Fahrenheit$/) do + #pending # express the regexp above with the code you wish you had + @converter.type = "Celcius" +end + +When(/^I press convert$/) do + @result = @converter.convert +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + @result.should eq "#{args1}.#{args2}" +end + +Given(/^we have a puppy$/) do + # pending # express the regexp above with the code you wish you had +end + +Given(/^its name is Fred$/) do + #pending # express the regexp above with the code you wish you had +end + +When(/^we pet the puppy$/) do + #pending # express the regexp above with the code you wish you had +end + +Then(/^the puppy wags its tail$/) do + #pending # express the regexp above with the code you wish you had +end + +Given(/^its name is Bella$/) do + #pending # express the regexp above with the code you wish you had +end + +When(/^we ring the bell$/) do + #pending # express the regexp above with the code you wish you had +end + +When(/^it wags its tail$/) do + #pending # express the regexp above with the code you wish you had +end + +Then(/^we must take it out$/) do + #pending # express the regexp above with the code you wish you had +end + +Then(/^then it will not pee on the floor$/) do + #pending # express the regexp above with the code you wish you had +end diff --git a/week7/homework/features/step_definitions/pirate_translator.rb b/week7/homework/features/step_definitions/pirate_translator.rb new file mode 100644 index 0000000..dbde0c1 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_translator.rb @@ -0,0 +1,14 @@ +class PirateTranslator + PIRATE_TRANSLATIONS = { + "Hello Friend" => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + } + def say(pirate_words) + @pirate_speak = get_pirate_translation(pirate_words) + end + def translate + @pirate_speak # + "\n Shiber Me Timbers You Scurvey Dogs!!" ##+ ' Shiber Me Timbers You Scurvey Dogs!!' + end + def get_pirate_translation(pirate_words) + PIRATE_TRANSLATIONS[pirate_words] + end +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..e9e808c 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,5 @@ -require 'rspec/mocks/standalone' -require 'rspec/expectations' +#require 'rspec/mocks/standalone' +#require 'rspec/expectations' Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end @@ -22,28 +22,28 @@ Given /^I have a started Tic\-Tac\-Toe game$/ do @game = TicTacToe.new(:player) - @game.player = "Renee" + @game.player = "David" end Given /^it is my turn$/ do - @game.current_player.should eq "Renee" + @game.current_player.should eq "David" end -Given /^the computer knows my name is Renee$/ do - @game.player.should eq "Renee" +Given /^the computer knows my name is David$/ do + @game.player.should eq "David" end Then /^the computer prints "(.*?)"$/ do |arg1| - @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.should_receive(:puts).with(arg1) # + @game.indicate_player_turn# end Then /^waits for my input of "(.*?)"$/ do |arg1| @game.should_receive(:gets).and_return(arg1) - @game.get_player_move + @game.get_player_move # end -Given /^it is the computer's turn$/ do +Given /^it is the computer''s turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" 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..7b53850 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,49 @@ +class TicTacToe + attr_accessor :player + #include @game.player_symbol, @game.computer_symbol + SYMBOLS = [:X,:O] + def initialize(player=nil,current_player=nil) + @player = player + #@current_player = current_player + @current_player = current_player || [:computer, :player].shuffle[0] + end + def welcome_player player=@player + "Welcome #{@player}" + end + def current_player + #[@game.player, "Computer"].should include @game.current_player + #player = @player.to_s.capitalize + ["#{@player}","Computer"].shuffle[0] + end + def player_symbol + @player_symbol = :X #SYMBOLS.sample + end + def computer_symbol symbol = @computer_symbol + @computer_symbol = SYMBOLS.reject{|p| p==@player_symbol}.first + end + def indicate_player_turn + puts "#{@player}'s Move:" + end + #def get_player_move + # gets("Move") + #end + def get_player_move + gets.chomp + end + + def player_move + puts 'player_move' + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def board + @board = :a1 + end + +end \ No newline at end of file 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..6e86ad3 --- /dev/null +++ b/week7/homework/features/step_definitions/tic_tac_toe.rb @@ -0,0 +1,32 @@ +class TicTacToe + attr_accessor :player + #include @game.player_symbol, @game.computer_symbol + SYMBOLS = [:X,:O] + def initialize(player=nil,current_player=nil) + @player = player + @current_player = current_player + end + def welcome_player player=@player + "Welcome #{@player}" + end + def current_player + #[@game.player, "Computer"].should include @game.current_player + player = @player.to_s.capitalize #["#{@player}","Computer"].sample + end + def player_symbol + @player_symbol = :X #SYMBOLS.sample + end + def computer_symbol symbol = @computer_symbol + @computer_symbol = SYMBOLS.reject{|p| p==@player_symbol}.first + end + def indicate_player_turn + puts "#{@player}'s Move:" + end + def get_player_move + gets("Move") + end + def board + @board = :a1 + end + +end \ No newline at end of file diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..429cf55 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -5,16 +5,16 @@ Feature: Tic-Tac-Toe Game Scenario: Begin Game Given I start a new Tic-Tac-Toe game - When I enter my name Renee - Then the computer welcomes me to the game with "Welcome Renee" + When I enter my name David + Then the computer welcomes me to the game with "Welcome David" And randomly chooses who goes first And who is X and who is O Scenario: My Turn Given I have a started Tic-Tac-Toe game And it is my turn - And the computer knows my name is Renee - Then the computer prints "Renee's Move:" + And the computer knows my name is David + Then the computer prints "David's Move:" And waits for my input of "B2" Scenario: Computer's Turn diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..3d6b5cd 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,31 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +"method_missing" allows you to respond to class definition and it's class hierarchy in different, customized ways. +A custom response or action based on a specific condition can be utilized when a "method_missing" method is defined. For example, in an emergency situation, the method defined with "method_missing" could perform specific action when the "method_missing" condition is met in that 'emergency' situation. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Singletons in Ruby lets you define methods that are specific to a particular object. When we create +a singleton method a new anonymous class is created and inserted as the first link in an object's inheritance chain, being the first place Ruby will look for a method definition, then the superclass, etc. It is here in the 'anonymous class' where the singleton lives. + +Eigenclass is an alternate name for this dynamically created anonymous class. The word 'eigen' comes from the German, roughly implying something like, "one's very own." + 3. When would you use DuckTypeing? How would you use it to improve your code? + +DuckTypeing is style of programming with it's basic premise being an object’s type is determined by what it can do, not by its class: "if an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck." Therefore, Ducktypeing simply questions the value in testing the class of an object, for instance, or to check the type of arguments in a method call. Checking types of arguments can constrain the method's flexibility. Though this not a blanket rule and there are valid situations to test argument types or type of class, etc. DuckTypeing poses the question to developers to consider whether you’re getting a real benefit from the extra code "to write and maintain." Applying the DuckTypeing approach will rely more on Ruby's inherit framework and potentially allowing less defensive coding or over-coding to achieve a program's objective. Not only would this be cleaner and more maintainable, but also potentially more efficient and performance wise. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +Basically, class methods are methods that are called on a class, often preceded with self. as self.method_name, or class << self def method_name, or within a class, def ClassName.method_name. Instance methods are methods that are called on an instance of a class, thus for an instance class a new instance must be created to use them (Classname.new) + +'class_eval' can only be called on classes or modules, and it 'evals' on the instances of the +classes or modules that create instance methods. 'class_eval' will fail if the class doesn't exist. + +'instance_eval' on the other hand evaluates code against a single object's instance. If we use instance_eval to define a method on a class, it will define a method for just that instance of class, not all classes. + 5. What is the difference between a singleton class and a singleton method? + +Singleton methods reside in the Singleton class, where as instance methods reside in the superclass. If defined, a Singleton is only utilized when the programmer wants to have class-level methods for unique objects within that class. Used extensively with metaprogramming, Singletons are often referred to as the implicit ancestor of every Ruby Object. When we want to create unique behavior for an individual object we can create singleton methods for a specific object in the Singleton class that are not shared by other instances of the object. These are singleton methods as they belong to the single object only. + diff --git a/week7/rspec/expectations.rb b/week7/rspec/expectations.rb new file mode 100644 index 0000000..e69de29 diff --git a/week8/class_materials/couch.rb b/week8/class_materials/couch.rb index a919c51..ec6e715 100644 --- a/week8/class_materials/couch.rb +++ b/week8/class_materials/couch.rb @@ -9,8 +9,17 @@ def initialize(pillows, cushions, dogs) define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end + define_method("show_colors_#{s}") do + puts "show colors" + instance_variable_get("@#{s}").each do color| puts color} + end + + + define_method("#{s.to_s.gsub('s','')}_colors)") do + instance_variable_get("@#{s}").each do color| puts color) end + # def to_str # "I am a Couch" # end @@ -30,6 +39,8 @@ def initialize(pillows, cushions, dogs) # self.send(meth) # end + + end \ No newline at end of file diff --git a/week8/class_materials/experient.rb b/week8/class_materials/experient.rb new file mode 100644 index 0000000..90b98c7 --- /dev/null +++ b/week8/class_materials/experient.rb @@ -0,0 +1,37 @@ +class Object + def call_chain + "Object" + end +end + +class Animal + def call_chain + "Animal.#{super}" + end +end + +module NamedThing + def call_chain + "NamedThing.#{super}" + end +end + +module Speaker + def call_chain + "Speaker.#{super}" + end +end + +class Person < Animal + include NamedThing + include Speaker + def call_chain + "Person.#{super}" + end +end + +class Renee < Person + def call_chain + "#{self}.#{super}" + end +end \ No newline at end of file diff --git a/week8/homework/features/step_definitions/tic-tac-toe-steps.rb b/week8/homework/features/step_definitions/tic-tac-toe-steps.rb new file mode 100644 index 0000000..0382327 --- /dev/null +++ b/week8/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -0,0 +1,124 @@ +require 'rspec/mocks/standalone' +require 'rspec/expectations' +Given /^I start a new Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new +end + +When /^I enter my name (\w+)$/ do |name| + @game.player = name +end + +Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1| + @game.welcome_player.should eq arg1 +end + +Then /^randomly chooses who goes first$/ do + [@game.player, "Computer"].should include @game.current_player +end + +Then /^who is X and who is O$/ do + TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol +end + +Given /^I have a started Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new(:player) + @game.player = "David" +end + +Given /^it is my turn$/ do + @game.current_player.should eq "David" +end + +Given /^the computer knows my name is David$/ do + @game.player.should eq "David" +end + +Then /^the computer prints "(.*?)"$/ do |arg1| + @game.should_receive(:puts).with(arg1) + @game.indicate_player_turn +end + +Then /^waits for my input of "(.*?)"$/ do |arg1| + @game.should_receive(:gets).and_return(arg1) + @game.get_player_move +end + +Given /^it is the computers turn$/ do + @game = TicTacToe.new(:computer, :O) + @game.current_player.should eq "Computer" +end + +Then /^the computer randomly chooses an open position for its move$/ do + open_spots = @game.open_spots + @com_move = @game.computer_move + open_spots.should include(@com_move) +end + +Given /^the computer is playing X$/ do + @game.computer_symbol.should eq :X +end + +Then /^the board should have an X on it$/ do + @game.current_state.should include 'X' +end + +Given /^I am playing X$/ do + @game = TicTacToe.new(:computer, :X) + @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 +end + +When /^"(.*?)" is not taken$/ do |arg1| + @old_pos.should eq " " +end + +Then /^it is now the computers turn$/ do + @game.current_player.should eq "Computer" +end + +When /^there are three Xs in a row$/ do + @game = TicTacToe.new(:computer, :X) + @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X +end + +Then /^I am declared the winner$/ do + @game.determine_winner + @game.player_won?.should be_true +end + +Then /^the game ends$/ do + @game.over?.should be_true +end + +Given /^there are not three symbols in a row$/ do + @game.board = { + :A1 => :X, :A2 => :O, :A3 => :X, + :B1 => :X, :B2 => :O, :B3 => :X, + :C1 => :O, :C2 => :X, :C3 => :O + } + @game.determine_winner +end + +When /^there are no open spaces left on the board$/ do + @game.spots_open?.should be_false +end + +Then /^the game is declared a draw$/ do + @game.draw?.should be_true +end + +When /^"(.*?)" is taken$/ do |arg1| + @game.board[arg1.to_sym] = :O + @taken_spot = arg1.to_sym +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 +end \ No newline at end of file diff --git a/week8/homework/features/step_definitions/tic-tac-toe.rb b/week8/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..d78595c --- /dev/null +++ b/week8/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,108 @@ +class TicTacToe + SYMBOLS = [:X,:O] + attr_accessor :player, :board, :status + def initialize(current_player=nil,game_symbols=nil) + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + @status = status + @current_player = current_player || [:computer, :player].shuffle[0] + choose_player_symbol(game_symbols) + end + def choose_player_symbol(game_symbols=nil) + game_symbols ||= SYMBOLS.shuffle[0] + @player_symbol = { + :computer => SYMBOLS.reject{|s| s==game_symbols}.first, + :player => game_symbols + } + end + def computer_symbol + @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def current_player + {:computer => "Computer", :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + def get_player_move + gets.chomp.capitalize + end + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + def computer_move + move = @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.shuffle[0] + @board[move] = computer_symbol + @current_player = :player + move + end + ### + def current_state + row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row2.size+"\n"+ + row3 + "-"*row3.size+"\n"+ + "******" + end + def determine_winner + person_spots = @board.select{|k,v| v==player_symbol} + computer_spots = @board.select{|k,v| v==computer_symbol} + @player_win = false + @computer_win = false + @player_win = check_winner(person_spots) + @computer_win = check_winner(computer_spots) + end + def check_winner(player_spots_selected) + return true if player_spots_selected.has_key?(:A1) && player_spots_selected.has_key?(:A2) && player_spots_selected.has_key?(:A3) + return true if player_spots_selected.has_key?(:B1) && player_spots_selected.has_key?(:B2) && player_spots_selected.has_key?(:B3) + return true if player_spots_selected.has_key?(:C1) && player_spots_selected.has_key?(:C2) && player_spots_selected.has_key?(:C3) + + return true if player_spots_selected.has_key?(:A1) && player_spots_selected.has_key?(:B1) && player_spots_selected.has_key?(:C1) + return true if player_spots_selected.has_key?(:A2) && player_spots_selected.has_key?(:B2) && player_spots_selected.has_key?(:C2) + return true if player_spots_selected.has_key?(:A3) && player_spots_selected.has_key?(:B3) && player_spots_selected.has_key?(:C3) + + return true if player_spots_selected.has_key?(:A1) && player_spots_selected.has_key?(:B2) && player_spots_selected.has_key?(:C3) + return true if player_spots_selected.has_key?(:A3) && player_spots_selected.has_key?(:B2) && player_spots_selected.has_key?(:C1) + end + def player_won? + !@player_win? false : true + end + def computer_won? + !@computer_win? false : true + end + + def draw? + !player_won? && !computer_won? + end + + def over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} + end + end \ No newline at end of file diff --git a/week8/homework/features/tic-tac-toe.feature b/week8/homework/features/tic-tac-toe.feature new file mode 100644 index 0000000..ad04774 --- /dev/null +++ b/week8/homework/features/tic-tac-toe.feature @@ -0,0 +1,57 @@ +Feature: Tic-Tac-Toe Game + As a game player I like tic-tac-toe + In order to up my skills + I would like to play agaist the computer + +Scenario: Begin Game + Given I start a new Tic-Tac-Toe game + When I enter my name David + Then the computer welcomes me to the game with "Welcome David" + And randomly chooses who goes first + And who is X and who is O + +Scenario: My Turn + Given I have a started Tic-Tac-Toe game + And it is my turn + And the computer knows my name is David + Then the computer prints "David's Move:" + And waits for my input of "B2" + +Scenario: Computer's Turn + Given I have a started Tic-Tac-Toe game + And it is the computers turn + And the computer is playing X + Then the computer randomly chooses an open position for its move + And the board should have an X on it + +Scenario: Making Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is not taken + Then the board should have an X on it + And it is now the computers turn + +Scenario: Making Bad Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is taken + Then computer should ask me for another position "B2" + And it is now the computers turn + +Scenario: Winning the Game + Given I have a started Tic-Tac-Toe game + And I am playing X + When there are three Xs in a row + Then I am declared the winner + And the game ends + +Scenario: Game is a draw + Given I have a started Tic-Tac-Toe game + And there are not three symbols in a row + When there are no open spaces left on the board + Then the game is declared a draw + And the game ends \ No newline at end of file diff --git a/week8/homework/play_game.rb b/week8/homework/play_game.rb new file mode 100644 index 0000000..ffd1177 --- /dev/null +++ b/week8/homework/play_game.rb @@ -0,0 +1,24 @@ +require './features/step_definitions/tic-tac-toe.rb' + +@game = TicTacToe.new +puts "What is your name?" +@game.player = gets.chomp +puts @game.welcome_player + +until @game.over? + case @game.current_player + when "Computer" + @game.computer_move + when @game.player + @game.indicate_player_turn + @game.player_move + #puts 'Current Player:' + #puts @current_player + end + puts @game.current_state + @game.determine_winner +end + +puts "You Won!" if @game.player_won? +puts "I Won!" if @game.computer_won? +puts "DRAW!" if @game.draw? diff --git a/week8/homework/questions.txt b/week8/homework/questions.txt new file mode 100644 index 0000000..3d6b5cd --- /dev/null +++ b/week8/homework/questions.txt @@ -0,0 +1,33 @@ + +Please Read Chapters 23 and 24 DuckTyping and MetaProgramming + +Questions: +1. What is method_missing and how can it be used? + +"method_missing" allows you to respond to class definition and it's class hierarchy in different, customized ways. +A custom response or action based on a specific condition can be utilized when a "method_missing" method is defined. For example, in an emergency situation, the method defined with "method_missing" could perform specific action when the "method_missing" condition is met in that 'emergency' situation. + +2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Singletons in Ruby lets you define methods that are specific to a particular object. When we create +a singleton method a new anonymous class is created and inserted as the first link in an object's inheritance chain, being the first place Ruby will look for a method definition, then the superclass, etc. It is here in the 'anonymous class' where the singleton lives. + +Eigenclass is an alternate name for this dynamically created anonymous class. The word 'eigen' comes from the German, roughly implying something like, "one's very own." + +3. When would you use DuckTypeing? How would you use it to improve your code? + +DuckTypeing is style of programming with it's basic premise being an object’s type is determined by what it can do, not by its class: "if an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck." Therefore, Ducktypeing simply questions the value in testing the class of an object, for instance, or to check the type of arguments in a method call. Checking types of arguments can constrain the method's flexibility. Though this not a blanket rule and there are valid situations to test argument types or type of class, etc. DuckTypeing poses the question to developers to consider whether you’re getting a real benefit from the extra code "to write and maintain." Applying the DuckTypeing approach will rely more on Ruby's inherit framework and potentially allowing less defensive coding or over-coding to achieve a program's objective. Not only would this be cleaner and more maintainable, but also potentially more efficient and performance wise. + +4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +Basically, class methods are methods that are called on a class, often preceded with self. as self.method_name, or class << self def method_name, or within a class, def ClassName.method_name. Instance methods are methods that are called on an instance of a class, thus for an instance class a new instance must be created to use them (Classname.new) + +'class_eval' can only be called on classes or modules, and it 'evals' on the instances of the +classes or modules that create instance methods. 'class_eval' will fail if the class doesn't exist. + +'instance_eval' on the other hand evaluates code against a single object's instance. If we use instance_eval to define a method on a class, it will define a method for just that instance of class, not all classes. + +5. What is the difference between a singleton class and a singleton method? + +Singleton methods reside in the Singleton class, where as instance methods reside in the superclass. If defined, a Singleton is only utilized when the programmer wants to have class-level methods for unique objects within that class. Used extensively with metaprogramming, Singletons are often referred to as the implicit ancestor of every Ruby Object. When we want to create unique behavior for an individual object we can create singleton methods for a specific object in the Singleton class that are not shared by other instances of the object. These are singleton methods as they belong to the single object only. + diff --git a/week9/class_materials/doc/Thing.html b/week9/class_materials/doc/Thing.html new file mode 100644 index 0000000..e332789 --- /dev/null +++ b/week9/class_materials/doc/Thing.html @@ -0,0 +1,195 @@ + + + + + + +class Thing - RDoc Documentation + + + + + + + + + + + + + + + + +
+

class Thing

+ +
+ +

encoding: UTF-8 This is a thing We are learning about rdoc

+
Author +
+

Renée

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

Public Instance Methods

+ + +
+ +
+ do_something() { || ... } + + click to toggle source + +
+ + +
+ +

This is an rDoc thing

+
  • +

    This method does this

    +
  • +

    And this

    +
+ + + + +
+
# File test.rb, line 12
+def do_something 
+        yield
+end
+
+ +
+ + + + +
+ + +
+ +
+ +
+ + + + diff --git a/week9/class_materials/doc/created.rid b/week9/class_materials/doc/created.rid new file mode 100644 index 0000000..8c4f6bd --- /dev/null +++ b/week9/class_materials/doc/created.rid @@ -0,0 +1,2 @@ +Tue, 03 Dec 2013 19:23:08 -0800 +test.rb Tue, 03 Dec 2013 18:53:57 -0800 diff --git a/week9/class_materials/doc/images/add.png b/week9/class_materials/doc/images/add.png new file mode 100755 index 0000000..6332fef Binary files /dev/null and b/week9/class_materials/doc/images/add.png differ diff --git a/week9/class_materials/doc/images/arrow_up.png b/week9/class_materials/doc/images/arrow_up.png new file mode 100755 index 0000000..1ebb193 Binary files /dev/null and b/week9/class_materials/doc/images/arrow_up.png differ diff --git a/week9/class_materials/doc/images/brick.png b/week9/class_materials/doc/images/brick.png new file mode 100644 index 0000000..7851cf3 Binary files /dev/null and b/week9/class_materials/doc/images/brick.png differ diff --git a/week9/class_materials/doc/images/brick_link.png b/week9/class_materials/doc/images/brick_link.png new file mode 100644 index 0000000..9ebf013 Binary files /dev/null and b/week9/class_materials/doc/images/brick_link.png differ diff --git a/week9/class_materials/doc/images/bug.png b/week9/class_materials/doc/images/bug.png new file mode 100644 index 0000000..2d5fb90 Binary files /dev/null and b/week9/class_materials/doc/images/bug.png differ diff --git a/week9/class_materials/doc/images/bullet_black.png b/week9/class_materials/doc/images/bullet_black.png new file mode 100644 index 0000000..5761970 Binary files /dev/null and b/week9/class_materials/doc/images/bullet_black.png differ diff --git a/week9/class_materials/doc/images/bullet_toggle_minus.png b/week9/class_materials/doc/images/bullet_toggle_minus.png new file mode 100644 index 0000000..b47ce55 Binary files /dev/null and b/week9/class_materials/doc/images/bullet_toggle_minus.png differ diff --git a/week9/class_materials/doc/images/bullet_toggle_plus.png b/week9/class_materials/doc/images/bullet_toggle_plus.png new file mode 100644 index 0000000..9ab4a89 Binary files /dev/null and b/week9/class_materials/doc/images/bullet_toggle_plus.png differ diff --git a/week9/class_materials/doc/images/date.png b/week9/class_materials/doc/images/date.png new file mode 100644 index 0000000..783c833 Binary files /dev/null and b/week9/class_materials/doc/images/date.png differ diff --git a/week9/class_materials/doc/images/delete.png b/week9/class_materials/doc/images/delete.png new file mode 100755 index 0000000..08f2493 Binary files /dev/null and b/week9/class_materials/doc/images/delete.png differ diff --git a/week9/class_materials/doc/images/find.png b/week9/class_materials/doc/images/find.png new file mode 100644 index 0000000..1547479 Binary files /dev/null and b/week9/class_materials/doc/images/find.png differ diff --git a/week9/class_materials/doc/images/loadingAnimation.gif b/week9/class_materials/doc/images/loadingAnimation.gif new file mode 100644 index 0000000..82290f4 Binary files /dev/null and b/week9/class_materials/doc/images/loadingAnimation.gif differ diff --git a/week9/class_materials/doc/images/macFFBgHack.png b/week9/class_materials/doc/images/macFFBgHack.png new file mode 100644 index 0000000..c6473b3 Binary files /dev/null and b/week9/class_materials/doc/images/macFFBgHack.png differ diff --git a/week9/class_materials/doc/images/package.png b/week9/class_materials/doc/images/package.png new file mode 100644 index 0000000..da3c2a2 Binary files /dev/null and b/week9/class_materials/doc/images/package.png differ diff --git a/week9/class_materials/doc/images/page_green.png b/week9/class_materials/doc/images/page_green.png new file mode 100644 index 0000000..de8e003 Binary files /dev/null and b/week9/class_materials/doc/images/page_green.png differ diff --git a/week9/class_materials/doc/images/page_white_text.png b/week9/class_materials/doc/images/page_white_text.png new file mode 100644 index 0000000..813f712 Binary files /dev/null and b/week9/class_materials/doc/images/page_white_text.png differ diff --git a/week9/class_materials/doc/images/page_white_width.png b/week9/class_materials/doc/images/page_white_width.png new file mode 100644 index 0000000..1eb8809 Binary files /dev/null and b/week9/class_materials/doc/images/page_white_width.png differ diff --git a/week9/class_materials/doc/images/plugin.png b/week9/class_materials/doc/images/plugin.png new file mode 100644 index 0000000..6187b15 Binary files /dev/null and b/week9/class_materials/doc/images/plugin.png differ diff --git a/week9/class_materials/doc/images/ruby.png b/week9/class_materials/doc/images/ruby.png new file mode 100644 index 0000000..f763a16 Binary files /dev/null and b/week9/class_materials/doc/images/ruby.png differ diff --git a/week9/class_materials/doc/images/tag_blue.png b/week9/class_materials/doc/images/tag_blue.png new file mode 100755 index 0000000..3f02b5f Binary files /dev/null and b/week9/class_materials/doc/images/tag_blue.png differ diff --git a/week9/class_materials/doc/images/tag_green.png b/week9/class_materials/doc/images/tag_green.png new file mode 100644 index 0000000..83ec984 Binary files /dev/null and b/week9/class_materials/doc/images/tag_green.png differ diff --git a/week9/class_materials/doc/images/transparent.png b/week9/class_materials/doc/images/transparent.png new file mode 100644 index 0000000..d665e17 Binary files /dev/null and b/week9/class_materials/doc/images/transparent.png differ diff --git a/week9/class_materials/doc/images/wrench.png b/week9/class_materials/doc/images/wrench.png new file mode 100644 index 0000000..5c8213f Binary files /dev/null and b/week9/class_materials/doc/images/wrench.png differ diff --git a/week9/class_materials/doc/images/wrench_orange.png b/week9/class_materials/doc/images/wrench_orange.png new file mode 100644 index 0000000..565a933 Binary files /dev/null and b/week9/class_materials/doc/images/wrench_orange.png differ diff --git a/week9/class_materials/doc/images/zoom.png b/week9/class_materials/doc/images/zoom.png new file mode 100644 index 0000000..908612e Binary files /dev/null and b/week9/class_materials/doc/images/zoom.png differ diff --git a/week9/class_materials/doc/index.html b/week9/class_materials/doc/index.html new file mode 100644 index 0000000..dfd956d --- /dev/null +++ b/week9/class_materials/doc/index.html @@ -0,0 +1,71 @@ + + + + + + +RDoc Documentation + + + + + + + + + + + + + + + + +
+

This is the API documentation for RDoc Documentation. +

+ + + + diff --git a/week9/class_materials/doc/js/darkfish.js b/week9/class_materials/doc/js/darkfish.js new file mode 100644 index 0000000..f26fd45 --- /dev/null +++ b/week9/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 + + + + + + + + + + +

Table of Contents - RDoc Documentation

+ + +

Classes/Modules

+ + +

Methods

+ + + + + diff --git a/week9/class_materials/exceptional_ruby.rb b/week9/class_materials/exceptional_ruby.rb new file mode 100644 index 0000000..d0e4930 --- /dev/null +++ b/week9/class_materials/exceptional_ruby.rb @@ -0,0 +1,30 @@ + class DLBError < Exception :where all error come from in ruby + #can be specific for a specific class + #Doc has list of all child errors from exception + #can grab backtrace from an error + #message on function - so if want custom messaging + def message + "DLB threw an error" + end + end + begin + #raise 'Yipes' + rais DLBError.new + File.open ('hello.txt') + rescue DLBError => e + puts e.message + rescue Errno::ENDENT=> e + puts e (puts e.class) + rescue IOError + puts specific message + ensure #(like finally) …try, catch, finally - always ensure that this runs when even if there is an error puts "hello world" -even if there are no errors + puts "hello world" + end + + catch :yipes do + i=0 + while i<10 + sleep (2) and throw :yipes if i ==5 #and lower precedence thatn && - want it here + puts i+=1 + end + end diff --git a/week9/class_materials/hello.rb b/week9/class_materials/hello.rb new file mode 100644 index 0000000..f865e9d --- /dev/null +++ b/week9/class_materials/hello.rb @@ -0,0 +1,10 @@ +require 'sinatra' +require 'sinatra/reloader' #now just refresh rather than restart server + +get '/' do #default route + " + +

Hello World!!! + + " +end \ No newline at end of file diff --git a/week9/class_materials/named_thing.rb b/week9/class_materials/named_thing.rb new file mode 100644 index 0000000..328d2df --- /dev/null +++ b/week9/class_materials/named_thing.rb @@ -0,0 +1,2 @@ +module NamedThing +end \ No newline at end of file