diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index a038c9d..a7f4d80 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -11,8 +11,13 @@ Instructions for Mid-Term submission and Git Review (10pts): Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? + +T - 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? +Floats are saved by the same object id, saves them as a singleton above 32 bits it doesn’t freeze it anymore +symbols are fixed strings, its a singleton, it is always the same symbol when you reference it.saves on memorygit +in contrast when you use strings a a differnt instance when referenced by the same characters - Are these two statements equivalent? Why or Why Not? 1. x, y = "hello", "hello" 2. x = y = "hello" diff --git a/midterm/mid_term_spec.rb b/midterm/mid_term_spec.rb index 87e5c74..73db16d 100644 --- a/midterm/mid_term_spec.rb +++ b/midterm/mid_term_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper.rb' +require_relative '../spec_helper.rb' require "#{File.dirname(__FILE__)}/turkey" describe Turkey do @@ -12,7 +12,7 @@ end it "should be a kind of animal" do - @turkey.kind_of?(Animal).should be_true + @turkey.kind_of?(Animal).should be_truthy end it "should gobble speak" do @@ -31,7 +31,7 @@ end it "should be a kind of dinner" do - @t_dinner.kind_of?(Dinner).should be_true + @t_dinner.kind_of?(Dinner).should be_truthy end # Use inject here @@ -40,7 +40,7 @@ end it "should provide a menu" do - @t_dinner.respond_to?(:menu).should be_true + @t_dinner.respond_to?(:menu).should be_truthy end context "#menu" do @@ -54,7 +54,7 @@ end it "should have vegetables" do - @t_dinner.menu[:veggies].should eq [:ginger_carrots , :potatoes, :yams] + @t_dinner.menu[:veggies].should eq [:ginger_carrots, :potatoes, :yams] end # Dinners don't always have dessert, but ThanksgivingDinners always do! diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..948d6a4 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,28 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is a data structure that interacts with its own code, in other words, anything we manipulate in Ruby. + 2. What is a variable? +A variable labels and holds data, like an integer or a string. + 3. What is the difference between an object and a class? +An object is a class instance and a class contains class instances. +Classes contain objects that interact with methods and each other. + 4. What is a String? +A string is a sequence of characters that are sometimes printable. In Ruby, there are many built-in methods +that can be used on strings. + 5. What are three messages that I can send to a string object? Hint: think methods +String#split, String#scan, String#chomp. + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +To define a String literal you can use single quotes (‘ ’), double quotes (“ “), %q, %Q, or here documents. +%q and %Q are the same as single quotes and double quotes. +Here documents are treated as a section of source code and they can be used to create multiline strings. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..d17634a 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -16,18 +16,18 @@ @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.length.should eq 66 + 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 + encode_result = @my_string.encoding + encode_result.should eq Encoding.find("UTF-8") end end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..7383b41 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,22 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A Hash has key-value pairs that can be any object and an Array uses only integers as an index. 2. When would you use an Array over a Hash and vice versa? +An Array is used when you have an ordered list and it's ok to have an integer for the index. Also, it's easier to use an Array for iteration. +A Hash is used when you want to store information using a key that is not an integer. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a collection of constants and methods. Enumerable has methods used for searching and sorting collections (i.e. Hash and Array). 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +A class can only inherit from one parent class, but by using Mixin Modules you can simulate multiple inheritance. 5. What is the difference between a Module and a Class? +<<<<<<< HEAD +Modules you can use across multiple classes. Modules are about functions and constants and classes are about objects and instances. +======= +Modules you can use across multiple classes. Modules are about functions and constants and classes are about objects and instances. +>>>>>>> 500e6f9d77bbce46d757bf3757289e7e5b1b69ae diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..5be35d2 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,28 @@ +class SimonSays + def echo answer + puts answer + end + def shout phrase + puts phrase.upcase + end + def repeat (text, num) + if num == nil + num = 1 + num.times do + puts text + end + else + num.times do + puts text + end + end + end + def start_of_word(text, num) + index = num - 1 + puts text[0..index] + + end + def first_word(text) + puts text.split(' ').first + end +end diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 4e6f5bc..0861cfb 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -1,5 +1,5 @@ # Hint: require needs to be able to find this file to load it -require_relative "simon_says.rb" +require_relative 'simon_says.rb' require_relative '../../spec_helper' describe SimonSays do diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..5334dae --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,41 @@ +class Calculator +<<<<<<< HEAD + + def sum input + if input == [] +======= + attr_accessor :collection, :num + + + def sum (collection) + if collection == [] +>>>>>>> fbdb37d62bfd89ad8328fe90edf6957bc997e776 + return 0 + else + total = input.inject(:+) + end + total +end + def multiply *input + if input.class == String + return input.to_a.inject(:*) + else + return input.inject(:*) + + end + + def pow (collection, num) + b = 1 + num.times{b *= collection} + end + def fac (num) + if num == 0 + return 1 + else + array = (1..num).to_a + return array.inject(:*) + end + end + end + end + end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..51e4e32 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -1,3 +1,4 @@ + Please Read: - Chapter 6 Standard Types - Review Blocks @@ -6,10 +7,20 @@ Please Read: 1. What is a symbol? +A symbol is a constant name that is guaranteed to be unique and contain the same value through out the entire program. + 2. What is the difference between a symbol and a string? +Symbols are immutable, where as strings are mutable. Symbols are often used as hash keys because they are immutable. + 3. What is a block and how do I call a block? +A block is code between do and end or between brackets. An iterator method can call a block. The format for a block is first you have a collection for the method to iterator over then you have the keyword “do” or a open curly bracket then the value to be passed in, then the code, and finally a closing curly bracket or and “end” keyword. + 4. How do I pass a block to a method? What is the method signature? +By using the keyword yield you can pass a block to a method. You can also pass parameters to a block and a method can receive values from a block using method. The method signature is the method name with the parameter list. + 5. Where would you use regular expressions? + +You can use a regular expression to test a string for a matching pattern, to extract a matching pattern from a string, and substitution. diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..ec33793 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,10 +4,25 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby reads a line from a standard input or files specified when gets or file.gets is used. +There are also iterators you can use to read files. IO#each_byte iterates over 8-bit bytes from the IO object. IO#each_line calls the block with each line from the file. IO#foreach opens the file to read, calls the block on one line from the file then closes the file. IO#read gets an entire file into a string or into an array of lines. + 2. How would you output "Hello World!" to a file called my_output.txt? +File.open(“my_output.txt”, “w”) do |file| + file.puts “Hello World!” +end + +puts File.read(“my_output.txt”) + 3. What is the Directory class and what is it used for? +the Directory class is a way to list files and directories in Ruby. You can make instances of the Dir class and you can iterate over an object of the Dir class. + 4. What is an IO object? +An IO object is a way for a ruby program to write to or read from an external resource. + 5. What is rake and what is it used for? What is a rake task? + +Rake is an internal Domain Specific Language based in Ruby. Rake has libraries that make it easy to do tasks that are common during the build and deploy process. You can define a rake task to customize it or if you don’t define a rake task then it defaults to test. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..a1439fd --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,19 @@ +class Worker +def self.work (*t) + if t == [] + yield + else + n = t[0] + num = 0 + while num < n + num += 1 + total = yield + end + total + + + +end + +end +end diff --git a/week7/homework/features/pirate.feature b/week7/homework/features/pirate.feature index 7de1a0b..b030c86 100644 --- a/week7/homework/features/pirate.feature +++ b/week7/homework/features/pirate.feature @@ -6,6 +6,6 @@ Ahoy matey!: Pirate Speak Heave to: The mighty speaking pirate Gangway! I have a PirateTranslator Blimey! I say 'Hello Friend' - Aye I hit translate + Aye I hit translate Let go and haul it prints out 'Ahoy Matey' - Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..8044f66 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,11 @@ +class PirateTranslator + + def say(arg) + @hi = "Ahoy Matey" + return @hi + end + + def translate + return "#{@hi}\n Shiber Me Timbers You Scurvey Dogs!!" + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/play_game.rb b/week7/homework/features/step_definitions/play_game.rb new file mode 100644 index 0000000..a8948fa --- /dev/null +++ b/week7/homework/features/step_definitions/play_game.rb @@ -0,0 +1,22 @@ +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 + 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/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..fed8b28 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| @@ -43,7 +43,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 +77,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 - @game = TicTacToe.new(:computer, :X) +When /^there are three Xs in a row$/ do + @game = TicTacToe.new(:player, :X) #computer @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 +105,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..2f1893d --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,156 @@ +class TicTacToe + attr_accessor :player + attr_accessor :board + attr_accessor :player_symbol + attr_accessor :computer_symbol + + SYMBOLS = [:O, :X] + + def initialize(first_player = nil, player_symbol = nil) + if player_symbol == nil + @player_symbol = :O + @computer_symbol = :X + else + @player_symbol = player_symbol + if @player_symbol == :X + @computer_symbol = :O + else + @computer_symbol = :X + end + end + + if first_player == nil + @c_player = [:player, :computer].sample + else + @c_player = first_player + end + @board = {:A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def player=(p) + @player = p + + end + + + def welcome_player + return "Welcome #{@player}" + + + end + + def current_player + if @c_player == :player + return @player + else + return "Computer" + end + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + player_move = gets.chomp + return player_move + end + + def player_move + move = get_player_move.to_sym + while @board[move] != " " + move = get_player_move.to_sym + end + @board[move] = @player_symbol + @c_player = :computer + return move + end + + def computer_move + c_move = open_spots.sample + @board[c_move] = @computer_symbol + @c_player = :player + return c_move + end + + def spots_open? + if open_spots.empty? + false + else + true + end + end + + + def open_spots + open_array = [] + @board.each do |k, v| + if v == " " + open_array.push(k) + end + end + open_array + end + + def determine_winner + in_a_row = [[:A1, :A2, :A3], + [:B1, :B2, :B3], + [:C1, :C2, :C3], + [:A1, :B1, :C1], + [:A3, :B3, :C3], + [:A1, :B2, :C3], + [:A3, :B2, :C1]] + if @c_player == :player + symbol = @player_symbol + else + symbol = @computer_symbol + end + in_a_row.each do |a| + first = a[0] + second = a[1] + third = a[2] + if @board[first] == symbol && @board[second] == symbol && @board[third] == symbol + puts "you won, #{symbol}" + if symbol == @player_symbol + return :player + else + return :computer + end + end + end + end + + def player_won? + determine_winner == :player + end + + def computer_won? + determine_winner == :computer + end + + def current_state + @board.to_s + end + + def draw? + if player_won? || computer_won? + return false + end + @board.each do |k, v| + if v == " " + return false + end + end + true + end + + def over? + true if determine_winner || draw? + end + + + +end diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..a63d919 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -1,7 +1,7 @@ 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 + I would like to play against the computer Scenario: Begin Game Given I start a new Tic-Tac-Toe game @@ -19,7 +19,7 @@ Scenario: My Turn Scenario: Computer's 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..8e5ba1c 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? + +If Ruby can't find the name of the method in the object's class ancestry then it looks for method_missing. Method_missing can be overridden and used in your own classes, so you can make your own error message in an application specific way. Method_missing can also be used to simulate accessors by putting the object in its singleton class. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +An Eigenclass is a singleton class. When you define a Singleton method for an object Ruby creates an anonymous class for the Singleton method called an Eigenclass. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +You can use DuckTypeing if you want more flexible code. For example, if you want to append something to a file, you can pass in a string, an array, or a file and #append will work the same. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +With class method, you can call the method on the class itself, where as with an instance method you have to create an instance of the class before you can call the method. +With Class_eval things are set in the body of the class and will be define an instance method. Where as instance_eval sets things up like you were in a singleton method of class self and thus any methods defined will be class methods. + + 5. What is the difference between a singleton class and a singleton method? + +A singleton method is unique to a single object a singleton class contains singleton methods.