diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1152c22..3a6295f 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -73,22 +73,23 @@ end context "Examples for in-class test exploration" do - it "should know order of operations" do - # Fix the Failing Test - # Order of Operations is Please Excuse My Dear Aunt Sally: - # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - ((((1+2)-5)*6)/2).should eq -6 - end - it "should count the characters in your name" do - "Tom".should have(3).characters - end + it "should know order of operations" do + # Fix the Failing Test + # Order of Operations is Please Excuse My Dear Aunt Sally: + # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction + (((1+2)-5)*(6/2)).should eq -6 + end - it "should check basic math" do - (40+2).should eq 42 + it "should count the characters in your name" do + "Matt".should have(4).characters + end + + it "should check basic math" do + (4+4-2).should eq 6 end - it "should check basic spelling" do - "Field".should include('ie') + it "should check basic spelling" do + "Matt".should include("Matt") end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..f36c970 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,43 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is an instance of a class that is represented in memory. + 2. What is a variable? +Variables holds a reference to an object or data. There are different types of variables with different scopes. + 3. What is the difference between an object and a class? +A class is the code or container that has properties ( methods / variable ) that can be used to create an instance of an object. A object is an instance of that class. + 4. What is a String? +A string are a sequence of characters, for example "Testing", "This is a string.", and even "324234". + 5. What are three messages that I can send to a string object? Hint: think methods +str.capitalize + This will capitalize a string + +str.downcase + returns a copy of the string where every uppercase character in the string is a lowercase + +str.hash + returns a hash of the string + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +Good lord, I've never seen so many ways to define a string literal in a language before. What is the best practice here? + +I'll the ones I find most interesting: + +%q and %Q can be used to create string literals, and the difference between them is the degree of substitution performed. + +%q is the equivalent to a single-quoted delimited string, and a substitution is performed by using a single "\". + +%q!This is a test string string! + +%Q is the equivalent to a double-quote delimited string, and quoting the book supports a "boatload" more escape sequences. They reference the most common one, the new line sequence "\n" + +%Q!Yada yada double quoted string! \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..3ffb40c 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,25 +1,20 @@ # 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 count the charaters" do + @my_string.should have(66).charaters + 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"))' + (@my_string.encoding).should eq (Encoding.find("UTF-8")) end end end diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index a6b943d..f81a3c3 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,27 +1,21 @@ class Book - attr_accessor :title - attr_reader :page_count - @@book_count = 0 + attr_accessor :pages, :title - def self.book_count - @@book_count - end + @@library_count = 0 - def initialize title = "Not Set", page_count = 0 - @@book_count += 1 - @page_count = page_count + def initialize pages = 0, title="N/A" + @pages = pages @title = title + @@library_count += 1 end - - def test - @test = "Hello!" + def self.library_count + @@library_count end - - def out_put_test - puts @test - puts @@book_count + + def happy + "There are #{@pages} happy pages in this book" end -end \ No newline at end of file +end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..ade4753 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,49 +1,33 @@ -require './book' +require './book.rb' -describe Book do +describe Book do - - context "::book_count" do + before :each do + @book = Book.new 542, "Programming Ruby" + end - it "should count how many books have been created" do - Book.new - Book.new - Book.new - Book.book_count.should eq 3 + context "::library_count" do + it "should tell us how many books we have" do + 34233.times{ Book.new 3 } + Book.library_count.should eq 34234 end - end - context "::new" do - - it "should set some defaults" do - Book.new.title.should eq "Not Set" + context "#pages" do + it "should have a pages" do + @book.should respond_to "pages" end - it "should allow us to set the page count" do - book = Book.new "Harry Potter", 5 - book.page_count.should eq 5 + it "should allow us to set the number" do + @book.pages = 542 + @book.pages.should eq 542 + end end - end - - context "#title" do - - before :each do - @book = Book.new + context "#title" do + it "should let us set and get a title" do + @book.title = "Programming Ruby" + @book.title.should eq "Programming Ruby" + end 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/questions.txt b/week2/homework/questions.txt index 939e42d..a401d54 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,22 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + An array is an ordered list of elements that are accessed using an index number. A Hash is a container of unordered elements that are stored in a map => key fashion. The values are accessed by calling the key, e.g Hash['map']. + 2. When would you use an Array over a Hash and vice versa? + Arrays are used when speed is important and elements need to be organized in an ordered fashion. Hashes are used when data needs to be accessed by a key, not the index. + 3. What is a module? Enumerable is a built in Ruby module, what is it? + A module is a library of reusable code that can be imported into other code. Modules can contain methods, classes, and constants. Emumerable is module that allows your classes to support many of Rubys popular methods like ".include?" + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + Yes you can, well, I'm just throwing this out there, I'm probably wrong :), but from my understanding inheritance looks like it works in a hierarchical fashion. So if you want to inherit from more than one class, you'll have to structure your classes in a manner that allow the inheritance to flow up the chain. I'm having trouble articulating this, so I'll try and draw it. + + Parent Class <--- Teir 1 class <---- Teir 2 class <----- Teir 3 classs + 5. What is the difference between a Module and a Class? + + A Module can't be used to create an instance. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..f5c39c9 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,27 @@ +module SimonSays + + # return the string + def echo(string) + "#{string}" + end + + # return string in all capitals + def shout(string) + "#{string.upcase}" + end + + # repeat the string a min of 2 times, unless specified + def repeat(string, repeat=2) + ("#{string} " * repeat).strip + end + + # return the charaters starting from start of string to length + def start_of_word(string, length) + "#{string[0, length]}" + end + + # return first word in a string using a space has a delimiter + def first_word(string) + "#{string.split(" ")[0]}" + end +end diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..6cc67fe --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,26 @@ +class Calculator + + def sum(*args) + args.flatten! + args.inject(0) {|sum, element| sum+element} + end + + def multiply(*args) + args.flatten! + args.inject(1) {|product, element| product*element} + end + # derp + def pow(x, y) + x ** y + end + + def fac(num) + # convert num to absolute value, then inject to find the factorial unless num == 0, then return 1 + ( (1..num.abs).inject { |factorial, n| factorial * n } unless num == 0 ) || ( 1 ) + end + + +end + + + diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..c7a2393 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,6 +1,6 @@ require "#{File.dirname(__FILE__)}/calculator" -describe Calculator do +describe Calculator do before do @calculator = Calculator.new diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..b818c18 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,30 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + They are similar to a string, and can be used to represent strings or names, but unlike strings they are created only once in memory and are immutable. 2. What is the difference between a symbol and a string? - + One simple difference is that symbols are immutable and strings are mutable. Symbols are only created once in memory, unlike strings. + 3. What is a block and how do I call a block? + A block is similar to a nameless function. They are always called in conjunction with a method, and wrapped in braces or a do/end. You can call a block by calling a method on a certain object, and then following up with brackets or the do/end. For example + + array = [1, 2, 3, 4, 5] + array.each { |n| puts n } + + Blocks are probably my favorite part of Ruby so far :) 4. How do I pass a block to a method? What is the method signature? + You can pass a block to a method by using the yield command. + + def testmethod + yield + end + + testmethod { puts "This text" } => "This text" + + The method signature is the method name, the arguments, and the order of the arugments. + 5. Where would you use regular expressions? + There are lots of places to use a regular expression, parsing log files, trying to match certain fields in text files, and finding hidden treasure. \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..857030e 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,11 +4,42 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +It reads by opening up a opening and closing IO streams, which allows you to read and write data to files, standard input, and standard output. + +One way to open a file is to use the File class, which is a subclass of the IO class. + +file = File.new("Matts.txt", "r") # this would open the file Matts.txt for reading only + 2. How would you output "Hello World!" to a file called my_output.txt? +First, you need to open the file for writing. + +file = File.new("my_output.txt", "w") +file.puts("Hello World!") +file.close + 3. What is the Directory class and what is it used for? +The Object Dir class is a class that allows you to manulape directories, list directories, and poke in side directories. It looks like its a great class for some shell shellish scripting :) + 4. What is an IO object? +The IO object is an object that allows you to open and close streams of bytes that allow to you read and write data. It has many subclasses that include File and Directory. + 5. What is rake and what is it used for? What is a rake task? +Rake is a build tool build and compile large software tools. It's very similar to make. + +A rake task is a rake task... For example, Here's an example I found on the internet: + +task :manipulate_files do + mkdir 'new_dir' + mv 'new_dir', 'lukasz' + chmod 0777, 'lukasz' + touch 'lukasz/wrobel.txt' +end + +We could use something like this to set up an install envirement for our ruby program. + + + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..9c60fac --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,11 @@ +class Worker + + # set x to nil, and then append yield to x for n times + # then return x + def Worker.work(n=1) + x = nil + n.times { x = yield } + x + end +end + diff --git a/week7/exercises/features/converter.feature b/week7/exercises/features/converter.feature index 5e262a8..f75df32 100644 --- a/week7/exercises/features/converter.feature +++ b/week7/exercises/features/converter.feature @@ -4,14 +4,14 @@ Feature: Converting metric I want to convert a Fahrenheit temperature to Celsius Scenario: - Given I have entered 32 into the converter - And I set the type to Fahrenheit + Given I have entered "32" into the converter + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 0.0 Scenario: - Given I have entered 75 into the converter - And I set the type to Fahrenheit + 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 diff --git a/week7/homework/features/step_definitions/piratetranslator.rb b/week7/homework/features/step_definitions/piratetranslator.rb new file mode 100644 index 0000000..8b8fe01 --- /dev/null +++ b/week7/homework/features/step_definitions/piratetranslator.rb @@ -0,0 +1,15 @@ +class PirateTranslator + + # derp + def say(english) + @english = english + end + + # herp + def translate + "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end + +end + + \ No newline at end of file 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..a9bd88e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..916b4e1 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,169 @@ +class TicTacToe + attr_accessor :board, :player, :player_symbol, :computer_symbol + SYMBOLS = [:O, :X] + + def initialize(current_player=nil, user_symbol=nil) + + unless current_player + @current_player = [:player, :computer].sample + else + @current_player = current_player + end + + unless user_symbol + random_symbol + else + @user_symbol = user_symbol + if @user_symbol == :X + @player_symbol = @user_symbol + @computer_symbol = :O + else + @player_symbol = @user_symbol + @computer_symbol = :X + end + end + + @board = { A1: ' ', A2: ' ', A3: ' ', + B1: ' ', B2: ' ', B3: ' ', + C1: ' ', C2: ' ', C3: ' ' } + + @winner = nil + + @win_rows = {one: [:A1, :A2, :A3], two: [:B1, :B2, :B3], three: [:C1, :C2, :C3], + four: [:A1, :B1, :C1], five: [:A2, :B2, :C2], six: [:A3, :B3, :C3], + seven: [:A1, :B2, :C3], eight: [:A3, :B2, :C1] + } + + end + + def determine_winner + @xarray = [] + @oarray = [] + + @board.each do |key, value| + @xarray << key if value == :X + @oarray << key if value == :O + end + + @win_rows.each do |win, spots| + if spots == spots & @xarray + if :X == @player_symbol + @winner = @player + else + @winner = "Computer" + end + elsif spots == spots & @oarray + if :O == @player_symbol + @winner = @player + else + @winner = "Computer" + end + else + draw? + end + end + end + + + + def open_spots + @open = [] + @board.each { |key, value| @open << key if value == ' '} + @open + end + + def computer_move + indicate_player_turn + @computer_move = @open.sample + @board[@computer_move] = @computer_symbol + @current_player = :player + @computer_move + end + + def random_start + [:player, :computer].sample + end + + def current_state + @current_game = "" + @game = @board.values.map { |value| value } + @game.each_slice(3) { |x| @current_game << "#{x.join("|")}\n_____\n" } + @current_game + end + + def indicate_player_turn + puts "#{current_player}'s Move:" + end + + def get_player_move + @pmove = gets.chomp + @pmove = @pmove.capitalize + @pmove + end + + def player_move + @move = get_player_move.to_sym + if open_spots.include?(@move) + @board[@move] = @player_symbol + else + spot_taken + player_move + end + @current_player = :computer + @move + end + + + def random_symbol + @player_symbol = SYMBOLS.sample + if @player_symbol == :X + @computer_symbol = :O + else + @computer_symbol = :X + end + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + @players = {player: @player, computer: "Computer"} + @players[@current_player] + end + + def spot_taken + puts "Try again, that spot is taken #{current_player}" + end + + def player_won? + if @winner == @player + true + end + end + + def computer_won? + if @winner == "Computer" + true + end + + end + + def spots_open? + !open_spots.empty? + end + + def draw? + if !computer_won? && !player_won? && !spots_open? + true + end + end + + def over? + if draw? || computer_won? || player_won? + true + end + end + + +end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..7b99f10 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -10,7 +10,7 @@ when "Computer" @game.computer_move when @game.player - @game.indicate_palyer_turn + @game.indicate_player_turn @game.player_move end puts @game.current_state diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..8d85b49 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,23 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +method_missing is a method that can be built into an object that gets called when a "unknown method" is called on the object. So instead of the program exiting, you can handle the error on your own. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +The Eigenclass is the "ghost class" that is created when a Singleton method is applied to a class. The objects orignal class is now a superclass, while the Eigenclass is the objects current class. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +You use DuckTyping when you want to code around what an object can do, not by its class. This will allow you remove lots of "type" checking you see in other languages, and less error handling! I think anyway! + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +A class method can be called without actually creating an instance of that class, for example the File Class has a lot of methods that can be used without creating an instance of that class. Instance methods belong to an instance of a class. + +instance_eval allows you to eval instance variables within a class while class_eval allows you to add methods within classes.. That's what I gather from my reading + 5. What is the difference between a singleton class and a singleton method? + +Is there a difference? It looks like they are both used to define certain methods to a certain object rather than writing those methods into its defiing class.