diff --git a/.gitignore b/.gitignore index bf3b80c..f5ebf1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store /*/.DS_Store /*/*/.DS_Store +/*/doc/* .ruby-version .ruby-gemset diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index a038c9d..ce7bb2d 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -9,17 +9,30 @@ Instructions for Mid-Term submission and Git Review (10pts): - Push your changes to the remote - After 6pm Thursday November 13th you will not be able to push to your remote repository (or clone). - Questions (20pts): - - What are the three uses of the curly brackets {} in Ruby? - - What is a regular expression and what is a common use for them? - - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? - - Are these two statements equivalent? Why or Why Not? - 1. x, y = "hello", "hello" - 2. x = y = "hello" -- What is the difference between a Range and an Array? -- 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? +Questions (20pts): + 1 - What are the three uses of the curly brackets {} in Ruby? + + + 2 - What is a regular expression and what is a common use for them? + + + 3 - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? + + + 4 - Are these two statements equivalent? Why or Why Not? + + +5 - What is the difference between a Range and an Array? + + +6 - Why would I use a Hash instead of an Array? + + +7 - What is your favorite thing about Ruby so far? + + +8 - What is your least favorite thing about Ruby so far? + Programming Problems (10pts each): - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. diff --git a/midterm/wish_list_spec.rb b/midterm/wish_list_spec.rb deleted file mode 100644 index 87f2813..0000000 --- a/midterm/wish_list_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "#{File.dirname(__FILE__)}/wish_list" - -describe WishList do - before :each do - @wish_list = WishList.new - @wish_list.wishes = ["Lamborghini", "Corn Starch and Water Moat", "Vegan Bacon Ice Cream", "Rubber Chicken", "Free Tickets to MockingJay"] - end - - it "should mixin Enumerable" do - @wish_list.is_a?(Enumerable).should be_true - end - - context "#each" do - it "should give me a numberd list" do - @wish_list.map{|w| w}.should eq ["1. Lamborghini", "2. Corn Starch and Water Moat", "3. Vegan Bacon Ice Cream", "4. Rubber Chicken", "5. Free Tickets to MockingJay"] - end - end -end \ No newline at end of file diff --git a/spec_helper.rb b/spec_helper.rb index 0f63c0c..83e7ea3 100644 --- a/spec_helper.rb +++ b/spec_helper.rb @@ -2,7 +2,11 @@ config.expect_with :rspec do |c| c.syntax = [:should, :expect] end + + config.color = true + config.mock_with :rspec do |mocks| mocks.syntax = :should end + end \ No newline at end of file diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index c4c5e0b..4821e2f 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'rspec/collection_matchers' require_relative '../../spec_helper' + describe "The Rspec ruby gem" do context "Domain Specific Language" do @@ -44,7 +45,7 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 2.should eq 2 end @@ -54,10 +55,10 @@ # The following expression is false. # However, this example PASSES because no expectation was created. - true == false + #true == false # The following line of code is correct, and would cause the example to fail: - # true.should == false + true.should == true # Lesson: It's easy to write bad tests. @@ -81,16 +82,32 @@ (1+2-5*6/2).should eq -12 end it "should count the characters in your name" do +<<<<<<< HEAD + "make a test to count the characters in your name" +======= +>>>>>>> d15be6bea7f47e9899c2d287761019ffd7532ba0 "Tom".should have(3).characters end it "should check basic math" do +<<<<<<< HEAD + "make a test to check some basic math" + (1+4+5).should eq 10 + end + + it "should check basic spelling" do + "make sure i is before e" + "field".should include("ie") + end + +======= (1+1).should eq 2 end it "should check basic spelling" do "field".should include("ie") end +>>>>>>> d15be6bea7f47e9899c2d287761019ffd7532ba0 end end \ No newline at end of file diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..22b60df 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,21 @@ 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 thing with properties and methods that can be used to do things. 2. What is a variable? + A variable is a space in memory where you can store a value. There are different types of variables based on the kind of data stored (integer, string, bignum) and the places in your app where you want to use the app (local, class, instance, etc.) 3. What is the difference between an object and a class? + An object is an instance of a class, a class is a blueprint for objects. 4. What is a String? + A string is a collection of one or more text/numbers that has a set of properties. 5. What are three messages that I can send to a string object? Hint: think methods + Methods for strings includ Upcase, Downcase, Reverse, length, chop, clear, an_array (to_a), empty?, strip, etc 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + (a) You can define a string by using double quotation marks: s = "this is a string" + (b) YOu can also define a string with single quotation marks, but you cannot escape special characters or use interpolation with #{..}: s = 'this is another string' + diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..148721e 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -16,18 +16,21 @@ @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" + it "should be able to count the charaters" do + @my_string.should have(66).characters + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @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"))' - encodeing #do something with @my_string here - #use helpful hint here + #helpful hint: should eq (Encoding.find("UTF-8"))' + #@my_string = @my_string.join(".") + @my_string.force_encoding("UTF-8") + result = @my_string.encoding #do something with @my_string here + result.should eq(Encoding.find("UTF-8")) end end end diff --git a/week2/examples/book.rb b/week2/examples/book.rb new file mode 100644 index 0000000..9b3cdc1 --- /dev/null +++ b/week2/examples/book.rb @@ -0,0 +1,27 @@ +class Book + + @@book_count = 0 + + #create a self. method so that if you change class name it cascades down + def self.book_count + @@book_count + end + + attr_accessor :title, :pages, :author + + def initialize title = "unknown", author: "uknown" , pages: 0 + @title = title + @pages = pages + @author = author + @@book_count += 1 + end + + def pages + @pages + end + + def book_count + @@book_count + end + +end \ No newline at end of file diff --git a/week2/examples/book_spec.rb b/week2/examples/book_spec.rb new file mode 100644 index 0000000..515a04e --- /dev/null +++ b/week2/examples/book_spec.rb @@ -0,0 +1,33 @@ +require_relative '../../spec_helper' +#when you say 'require' it's like running this code right here +#require knows about the .rb extension +require_relative 'book' + + +describe Book do + + #create a new instance of book class + before :each do + @my_book = Book.new + end + + it "should have a title" do + #see if it should respond to title + @my_book.should respond_to? "title" + + end + + it "should not have be nil" do + @my_book.title.should_not be_nil + end + + it "Should have a non empty title" do + @my_book.title.size.should > 0 + end + + it "should let me set the title" do + @my_book.title = "Harry Potter" + @my_book.title.should eq "Harry Potter" + end + +end \ No newline at end of file diff --git a/week2/examples/mad_libs.rb b/week2/examples/mad_libs.rb new file mode 100644 index 0000000..e3ce7b9 --- /dev/null +++ b/week2/examples/mad_libs.rb @@ -0,0 +1,4 @@ + +puts "enter a noun:" +noun = gets.chomp +puts "#{noun} is cool" \ No newline at end of file diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index 4662c1a..c380b29 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -3,6 +3,11 @@ describe Book do + + it "should count the books in our library" do + Book.book_count.should eq 1 + end + context "::book_count" do it "should count how many books have been created" do @@ -37,11 +42,22 @@ @book.should respond_to "title" end + + it "should have a default title of unknown" do + @my_book.title should eq "unknown" + end + it "should allow me to set the title" do @book.title = "Snow Crash" @book.title.should eq "Snow Crash" end + it "should allow us to set the title on a new instance" do + book = Book.new "Programming Ruby" + + book.title.should eq "Programming Ruby" + end + end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..fe38d7e 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -1,13 +1,20 @@ Please Read The Chapters on: -Containers, Blocks, and Iterators -Sharing Functionality: Inheritance, Modules, and Mixins +Containers, Blocks, and Iterators -- ch4 +Sharing Functionality: Inheritance, Modules, and Mixins -- ch 5 1. What is the difference between a Hash and an Array? + NTR: An array is an ordered list that can be referenced based on positions. A hash is a list of key - value pairs, which can be referenced by the key value. 2. When would you use an Array over a Hash and vice versa? + NTR: Use an array when you a list of values to store and speed / perfromance is critical. Use a hash when you have a value you want to refer to something else. 3. What is a module? Enumerable is a built in Ruby module, what is it? + NTR: A module is a collection of classes and functions that can be added into another class or module as a mixin. + Enumerable allows you to cycle through elements of elements (lists of items). YOu can use it with each or map or collect to go to every item in the list and perform some kind of check/operation. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + NTR: One child class can have one parent class. You can use modules to get around this by including them within the code with i mixin. 5. What is the difference between a Module and a Class? + NTR: At an implementation level you use the word Module instead of Class to instantiate. At an implementation level, a class is a thing, and a module is way to add more functionality to your thing without inheriting from another class. It also prevents namespace clashes by securing names within seperate realms that can then be separated with dot notation (module.function1 vs. class.funtion1). \ No newline at end of file diff --git a/week2/homework/sim_says.rb b/week2/homework/sim_says.rb new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/week2/homework/sim_says.rb @@ -0,0 +1 @@ + diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..c5c4f20 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,41 @@ + +module SimonSays + + def echo(msg) + msg + end + + def hello + puts "Hello" + end + + def shout(msg) + msg.upcase + end + + def repeat(msg, count=2) + str = "" + count.times do + str += "#{msg} " + end + str.strip + + #another way to solve this + str = [] + count.times do + str.push(msg) + end + str.join(" ") + end + + def start_of_word(word, length) + word[0, length] + end + + def first_word(phrase) + phrase.split(' ')[0] + end + + +end + diff --git a/week2/homework/simonsays.rb b/week2/homework/simonsays.rb new file mode 100644 index 0000000..e69de29 diff --git a/week3/exercises/monster.rb b/week3/exercises/monster.rb index 013c3d2..6c6d44f 100644 --- a/week3/exercises/monster.rb +++ b/week3/exercises/monster.rb @@ -11,4 +11,8 @@ def initialize(noc, legs, name="Monster", vul = [], dangers = []) @dangers = dangers @legs = legs end + + def attack human + "#{name} attacked #{human.name}" + end end diff --git a/week3/exercises/monsters.rb b/week3/exercises/monsters.rb index 0635daa..2da1517 100644 --- a/week3/exercises/monsters.rb +++ b/week3/exercises/monsters.rb @@ -35,3 +35,27 @@ :vulnerabilities => ['CO2', 'ice', 'cold'], :legs => 0 } + +#how many monsters are nocturnal? +puts $monsters.collect{|m| m[:nocturnal]} + +#waht name of monsters that are nocturnal +puts $monsters.select{|m| m[:nocturnal]}.map{|m| m[:name]} + +#how many legs do monsters have? +puts $monsters.map{|m| m[:legs]}.inject(:+) + +#what are 2 most common dangers + +dangers = $monsters.flat_map{|m| m[:dangers]} +dangers.inject(Hash.new(0)) do |h, d| + h[d] += 1 + h +end + +dangers.group_by{|i| i} + + +#what are 2 most common vulnerabilities? + + diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..3ae069a --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,24 @@ + +class Calculator + + def sum(arr) + arr.inject(0) {|sum, n| sum + n} + end + + def multiply(*arr) + arr.flatten.inject {|product, n| product * n} + end + + def pow (base, power) + #take variable 1 and raise it to power of variable 2 + base**power + end + + def fac(val) + #array = each item times all the previous items + total = 1 + val.downto(1) { |n| total *= n } + total + end + +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 6ae227b..a03012f 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -30,19 +30,21 @@ # write tests and code for the following: describe "#multiply" do it "multiplies two numbers" do - @calculator.multiply(2,2).should eq 4 - end + @calculator.multiply(2,2).should eq 4 + end it "multiplies an array of numbers" do - @calculator.multiply([2,2]).should eq 4 + @calculator.multiply([2,2]).should eq 4 end end - it "raises one number to the power of another number" do - p = 1 - 32.times{ p *= 2 } - @calculator.pow(2,32).should eq p - end + describe "#power" do + it "raises one number to the power of another number" do + p = 1 + 32.times{ p *= 2 } + @calculator.pow(2,32).should eq p + end + end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..7a03a87 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,17 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + A: A symbol is an object that is used to reference something else. 2. What is the difference between a symbol and a string? + A: A symbol is immutable, which means it should not change. This makes it a bit faster to process in the system. In >2.2 versions of Ruby, symbols also can cause memory leaks if you use them for items that change because there was no symbol garbage collection to get rid of unused symbols. 3. What is a block and how do I call a block? + A: A block is a chunk of code grouped together either by do end or by braces {}. You call a block by using the block name. + +4. How do I pass a block to a method? What is the method signature? + A: You can invoke a block using the yield statement from within a method. You can also prefix the last arugument in a method signature with an ampersand to create a proc object from the bloc. Can then execute this object with the .call method. -4. How do I pass a block to a method? What is the method signature? 5. Where would you use regular expressions? + A: Use regular expressions when you want to analyze data and do something based on the contents of the data. For example you can replace items, you can pull items out and transform them, you can trigger actions based on the contents, and you can use contents to turn things on or off based on the contents. diff --git a/week4/exercises/odd_number.rb b/week4/exercises/odd_number.rb new file mode 100644 index 0000000..00a459c --- /dev/null +++ b/week4/exercises/odd_number.rb @@ -0,0 +1,20 @@ +class OddNumber + + include Comparable + attr_reader :value + + #create odd number and initialize it with value + def initialize value + @value = value + end + + #when i have an instance, i get back a successor of this odd number (next one) + def succ + OddNumber.new(@value + 2) + end + + def <=> other_odd_number + value <=> other_odd_number.value + end + +end \ No newline at end of file diff --git a/week4/exercises/timer.rb b/week4/exercises/timer.rb new file mode 100644 index 0000000..ac9c374 --- /dev/null +++ b/week4/exercises/timer.rb @@ -0,0 +1,12 @@ +#until you have a test taht drives you to have an instance you should use module +#if we wanted the last time it was run, then you'd have a class + +module Timer + + def self.time_code n = 1 + start_time = Time.now + n.times{yield} + Time.now - start_time + end + +end diff --git a/week4/exercises/timer_spec.rb b/week4/exercises/timer_spec.rb new file mode 100644 index 0000000..c6e4455 --- /dev/null +++ b/week4/exercises/timer_spec.rb @@ -0,0 +1,33 @@ +require_relative '../../spec_helper' +require_relative 'timer' + +describe Timer do + + it "should run our code" do + flag = false + Timer.time_code do + flag = true + end + expect(flag).to be_true + flag.should eq true + end + end + + it "should return the run time for our code" do + + run_time = Timer.time_code do + sleep(3) + nil + end + run_time.should be_within(0.1).of(3.0) + end + + + it "should run code mulitlpe times" do + count = 0 + Timter.time_code(10) do + count += 1 + end + count.should eq 10 + end +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..5603f29 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,11 +3,22 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +A: create an IO Object and open with 'r' or r+ permissions set. Use readline to grab a line. Can also use iterators such as each_byte to iterate over contents. 2. How would you output "Hello World!" to a file called my_output.txt? +A: Use the puts method + +file.open("my_output.txt", "w") do |file| + file.puts "Hello world!" +end 3. What is the Directory class and what is it used for? +A: Dir is the class and it is used to represent the file system and let you list the files and their contents. + 4. What is an IO object? +A: AN IO object is used for managing input and output connections. 5. What is rake and what is it used for? What is a rake task? +A: Rake is a domain specific langauge that lets you define tasks that can be run at the command line. You can do things like migrate database information and clearing cache. + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..406487e --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,7 @@ +class Worker + def self.work(n=1) + result = nil + n.times {result = yield} + result + end +end \ No newline at end of file diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..c032cab --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,67 @@ + +#read lines and output them +desc "read files and print them" +task :readnames do + f.File.open("names", 'r') + f.each do |line| + puts "#{line}" + end +end + +desc "create a class directory" +task :makedirectory do + d = Dir.new("newdirectory") +end + +desc "make directory for each name" +task :makenamedirectories => [m:makedirectory] do + x.each do |line| + x.mkdir("#{|line|}") + end +end + + +==== + +desc "prints all student names" +task :print_names do + File.open("names") do |f| + f.each do |name| + #see the \n new line as .inspect of objects + p name.chomp + #don't see new line + puts name + print name + end + end +end + +desc "Creates a class directory" +task :create_class_dir do + Dir.mkdir "class" unless Dir.exists? "class" +end + +desc "creates student direcotries" +task :create_student_dirs => [:create_class_dir] do + File("names") do |f| + f.deach do |name| + dir = "class/#{name.chomp}" + Dir.mkdir(dir) unless Dir.exists? dir + end + end +end + +desc "removes all directories and the class directory" +task :clean_up => [:create_student_dirs] do + File.open("names" do |f| + f.each do |name| + dir = "class/#{name.chomp}" + " + Dir.rmdir("class") +end + + + + + + diff --git a/week6/homework/natron_test_gem.gemspec/,rspec b/week6/homework/natron_test_gem.gemspec/,rspec new file mode 100644 index 0000000..c3e47b9 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/,rspec @@ -0,0 +1,3 @@ +--color + +--format documentation \ No newline at end of file diff --git a/week6/homework/natron_test_gem.gemspec/.gitignore b/week6/homework/natron_test_gem.gemspec/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/week6/homework/natron_test_gem.gemspec/.travis.yml b/week6/homework/natron_test_gem.gemspec/.travis.yml new file mode 100644 index 0000000..0b96ff0 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/.travis.yml @@ -0,0 +1,8 @@ +language: ruby +rvm: +- 2.1.1 +- 1.8.7 +- 1.9.2 +- 1.9.3 +- jruby-18mode - jruby-19mode +- ruby-head \ No newline at end of file diff --git a/week6/homework/natron_test_gem.gemspec/bin/natron_test b/week6/homework/natron_test_gem.gemspec/bin/natron_test new file mode 100644 index 0000000..a717749 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/bin/natron_test @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +require 'natron_test_gem' +puts "hello hello" diff --git a/week6/homework/natron_test_gem.gemspec/gemfile b/week6/homework/natron_test_gem.gemspec/gemfile new file mode 100644 index 0000000..61061eb --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'rspec' +gem 'rake' \ No newline at end of file diff --git a/week6/homework/natron_test_gem.gemspec/lib/natron_test_gem.rb b/week6/homework/natron_test_gem.gemspec/lib/natron_test_gem.rb new file mode 100644 index 0000000..b3ed6e2 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/lib/natron_test_gem.rb @@ -0,0 +1,8 @@ + + +class Hello + def self.hi + puts "hello world it's natron's test gem" + end +end + diff --git a/week6/homework/natron_test_gem.gemspec/natron_test_gem-0.0.0.gem b/week6/homework/natron_test_gem.gemspec/natron_test_gem-0.0.0.gem new file mode 100644 index 0000000..89225b3 Binary files /dev/null and b/week6/homework/natron_test_gem.gemspec/natron_test_gem-0.0.0.gem differ diff --git a/week6/homework/natron_test_gem.gemspec/natron_test_gem.gemspec b/week6/homework/natron_test_gem.gemspec/natron_test_gem.gemspec new file mode 100644 index 0000000..20891c7 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/natron_test_gem.gemspec @@ -0,0 +1,14 @@ +Gem::Specification.new do |s| + s.name = 'natron_test_gem' + s.version = '0.0.0' + s.date = '2014-11-06' + s.summary = "Making a Test Gem" + s.authors = ["Nathan Rohm"] + s.email = 'nathan19@hotmail.com' + s.homepage = 'http://nathanrohm.com' + s.description = 'Test gem' + s.license = 'MIT' + s.files = ["lib/natron_test_gem.rb"] + s.executables << ['natron_test'] +end + diff --git a/week6/homework/natron_test_gem.gemspec/spec/Rakefile.rb b/week6/homework/natron_test_gem.gemspec/spec/Rakefile.rb new file mode 100644 index 0000000..7366f23 --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/spec/Rakefile.rb @@ -0,0 +1,6 @@ +require 'rspec/core/rake_task' + +RSPec::Core::RakeTask.new('spec') + +task :default => :spec + diff --git a/week6/homework/natron_test_gem.gemspec/spec/natron_test_rspec.rb b/week6/homework/natron_test_gem.gemspec/spec/natron_test_rspec.rb new file mode 100644 index 0000000..31ed4ea --- /dev/null +++ b/week6/homework/natron_test_gem.gemspec/spec/natron_test_rspec.rb @@ -0,0 +1,8 @@ +require_relative '../lib/natron_test_gem.rb' + +describe Hello + + it 'should say something cool' do + hello.hi == "hello world it's natron's test gem" + end +end diff --git a/week7/exercises/features/converter.feature b/week7/exercises/features/converter.feature index 5e262a8..fe268ae 100644 --- a/week7/exercises/features/converter.feature +++ b/week7/exercises/features/converter.feature @@ -13,5 +13,4 @@ Scenario: Given I have entered 75 into the converter And I set the type to Fahrenheit When I press convert - Then the result returned should be 23.9 - + Then the result returned should be 23.9 \ No newline at end of file diff --git a/week7/exercises/features/converter.rb b/week7/exercises/features/converter.rb new file mode 100644 index 0000000..359479f --- /dev/null +++ b/week7/exercises/features/converter.rb @@ -0,0 +1,8 @@ +class Converter + attr_accessor :value + attr_writer :units + + def convert + 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..09cddca --- /dev/null +++ b/week7/exercises/features/converter_steps.rb @@ -0,0 +1,19 @@ +Given (/^I have a convert$/) do + @converter = Converter.new +end + +Given(/^I have entered (\d+) into the converter$/) do |value| + @converter.value = value.to_f +end + +Given(/^I select Celsius$/) do + @converter.type = "Celsius" +end + +When(/^I (press|click) convert$/) do + @converter.convert +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + @converter.value.should eq "#{arg1}.#{arg2}".to_f +end \ No newline at end of file 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..93b73cd --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_translator.rb @@ -0,0 +1,13 @@ +class PirateTranslator + + def say(msg) + msg + end + + def translate + "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end + + +end + diff --git a/week7/homework/features/step_definitions/tic tac toe notes.txt b/week7/homework/features/step_definitions/tic tac toe notes.txt new file mode 100644 index 0000000..2b632dd --- /dev/null +++ b/week7/homework/features/step_definitions/tic tac toe notes.txt @@ -0,0 +1,10 @@ + +create o list +create x list + +iterate through board hash if any values equal 0 add to o list +same for x list + +iterate through list of winning combos, and see if o list contains them +same for x list + 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..d501e4c 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,5 +1,6 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' + Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end @@ -35,7 +36,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| @@ -43,7 +44,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -77,22 +78,22 @@ @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +Then /^it is now the computers turn$/ do @game.current_player.should eq "Computer" end -When /^there are three X's in a row$/ do +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 + @game.player_won?.should be_truthy end Then /^the game ends$/ do - @game.over?.should be_true + @game.over?.should be_truthy end Given /^there are not three symbols in a row$/ do @@ -105,11 +106,11 @@ end When /^there are no open spaces left on the board$/ do - @game.spots_open?.should be_false + @game.spots_open?.should be_falsey end Then /^the game is declared a draw$/ do - @game.draw?.should be_true + @game.draw?.should be_truthy end When /^"(.*?)" is taken$/ do |arg1| 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..b3f9af0 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,134 @@ +class TicTacToe + attr_accessor :player, :player_symbol, :computer_symbol, :board, :winning_marker + SYMBOLS = [:X, :O] + + def initialize(first_player = nil, marker = nil) + + init_marker(marker) + + @board = {A1: " ", A2: " ", A3: " ", + B1: " ", B2: " ", B3: " ", + C1: " ", C2: " ", C3: " "} + + @current_player_indicator = init_current_player(first_player) + @winning_marker = nil + end + + def init_marker(marker) + if marker == nil + @player_symbol = SYMBOLS.sample + else + @player_symbol = marker + end + remaining_symbol = SYMBOLS - [@player_symbol] + @computer_symbol = remaining_symbol.last + end + + def init_current_player(first_player) + if first_player == nil + return [:player, :computer].sample + else + return first_player + end + end + + def current_player + i = @current_player_indicator + if i == :player + @player + else + "Computer" + end + end + + def open_spots + open_spaces = [] + @board.each do |k, v| + if v == ' ' + open_spaces.push(k) + end + end + open_spaces + end + + def player_move + begin + make_move = get_player_move + end while board[make_move.to_sym] != " " + board[make_move.to_sym] = @player_symbol + @current_player_indicator = :computer + make_move.to_sym + end + + def get_player_move + make_move = gets.chomp + end + + def computer_move + make_move = open_spots.sample + board[make_move.to_sym] = @computer_symbol + @current_player_indicator = :player + make_move + end + + def current_state + "#{board.values{|v| v + ','}}" + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def spots_open? + true unless open_spots.length == 0 + end + + def player_won? + winning_marker == player_symbol + end + + def computer_won? + winning_marker == computer_symbol + end + + def draw? + winning_marker == nil && open_spots.count == 0 + end + + def over? + winning_marker != nil || draw? + end + + + def determine_winner + o_positions = [] + x_positions = [] + + @board.each do |key, val| + if val == :X + x_positions.push(key) + elsif val == :O + o_positions.push(key) + end + end + + winning_patterns = [[:A1,:A2,:A3],[:B1,:B2,:B3],[:C1,:C2,:C3], + [:A1,:B1,:C1],[:A2,:B2,:C2],[:A3,:B3,:C3], + [:A1,:B2,:C3],[:A3,:B2,:C1]] + + winning_patterns.each do |pattern| + if (o_positions & pattern).count == 3 + @winning_marker = :O + end + + if (x_positions & pattern).count == 3 + @winning_marker = :X + end + end + end + + + def welcome_player + "Welcome #{@player}" + 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..40d3cbd 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -17,9 +17,9 @@ Scenario: My Turn Then the computer prints "Renee's Move:" And waits for my input of "B2" -Scenario: Computer's Turn +Scenario: Computers Turn Given I have a started Tic-Tac-Toe game - And it is the computer's turn + 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 @@ -31,7 +31,7 @@ Scenario: Making Moves 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 computer's turn + And it is now the computers turn Scenario: Making Bad Moves Given I have a started Tic-Tac-Toe game @@ -40,12 +40,12 @@ Scenario: Making Bad Moves 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 computer's turn + 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 X's in a row + When there are three Xs in a row Then I am declared the winner And the game ends diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..dd899f0 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,12 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + A: Method missing is something you set on an object. You set it to return when an unanswerable message is sent to the object. It helps you handle such requests more gracefully then just sending a NoMethodError exception. You set it with def method_missing(m, *args, &block) [message] end 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + A: An Eigenclass is a dynamically created anonymous class that is inserted into the method lookup path when an individual object is altered but the parent class is not. Singleton methods live in the objects not the classes. 3. When would you use DuckTypeing? How would you use it to improve your code? + A: You would use ducktyping when you want to change the behavior of an object without creating a new class or altering an existing one. You would use this to make your existing code cleaner without introducing too many layers of unnecessary inheritance. 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + A: A class method is available from a class itself. Instance methods are called on an instance of a class. CLass_eval allows you to call a method within the person class without having to open the class. The instance_eval does something similar but works on an instance of the class, not the class itself. 5. What is the difference between a singleton class and a singleton method? + A: A singleton class is available for all instances of that anonymous class. A singleton method might be an anonymous method assigned to a class but not by other instances of that class. diff --git a/week8/exercises/couch_spec.rb b/week8/exercises/couch_spec.rb index 8af3ee0..71b78ac 100644 --- a/week8/exercises/couch_spec.rb +++ b/week8/exercises/couch_spec.rb @@ -9,7 +9,7 @@ end it "should tell me the cushions colors" do - @couch.cushions_colors.should eq ["grey", "grey"] + @couch.cushion_colors.should eq ["grey", "grey"] end it "should tell me the dogs names" do diff --git a/week9/exceptions.rb b/week9/exceptions.rb new file mode 100644 index 0000000..49ee873 --- /dev/null +++ b/week9/exceptions.rb @@ -0,0 +1,36 @@ +class ReneeError < Exception + + def message + "Renee breaks everything" + end + +end + + +def hello + begin + raise ReneeError.new + puts yield + rescue LocalJumpError => e + puts e + rescue ReneeError => e + puts e + puts "Don't let Renee near your code" + ensure + puts "you ran the hello method" + end + + + catch :renee_error do + puts "before throw" + throw :renee_error + puts "after throw" + end + + puts "outside of catch" + +end + +hello { " Hello World " } + +hello \ No newline at end of file diff --git a/week9/rdoc_demo.rb b/week9/rdoc_demo.rb new file mode 100644 index 0000000..13854e4 --- /dev/null +++ b/week9/rdoc_demo.rb @@ -0,0 +1,23 @@ +class HeadGear +end + +# This is a really cool Hat class +# +# Author:: Renée Hendricksen +# License:: MIT +# +# This is more info about the class +class Hat < HeadGear + + attr_accessor :color, :size + + # This is the constructor + # * size : The size of the Hat + # * color : The color of the Hat + def initialize color, size + @color = color + @size = size + end + + +end \ No newline at end of file diff --git a/week9/sinatra_test.rb b/week9/sinatra_test.rb new file mode 100644 index 0000000..58dd58d --- /dev/null +++ b/week9/sinatra_test.rb @@ -0,0 +1,19 @@ +require 'sinatra' +require 'thin' + +get '/hello' do + hello do |time| + "
Hello World