From 108649813a74a616b3b7e683da2f7605d2ae0a93 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Mon, 14 Oct 2013 08:18:19 -0700 Subject: [PATCH 1/7] The questions and tests were answered and updated so that they can be evalutaed by the teacher. --- week1/homework/questions.txt | 7 +++++++ week1/homework/strings_and_rspec_spec.rb | 13 +++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..e960565 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,20 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? + In Ruby everything is an object. An object is created either directly or indirectly by a class, and contains attributes and methods based upon it's class. It also contains the state of it's attributes. 2. What is a variable? + A variable is a reference to an object. 3. What is the difference between an object and a class? + A class is a blueprint from which individual objects can be created. Classes define the type of data and methods that an object will contain, whereas an object is an instance of the class it inherited from, containing it's own data and state. 4. What is a String? + A string is an object from the class String, that can hold printable characters or binary data. 5. What are three messages that I can send to a string object? Hint: think methods + chomp, split, squeeze. 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + A string literal can be defined using either single quotes or double quotes. The difference is that when using double quotes, the content of the string supports more escape sequences as well as interpolation, meaning that you can substitute the value of Ruby code, such as an instance variable, in the string if it is prepended with # and, depending on whether the code to insert is a global, class or instance variable,) wrapping the code in {}. + diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..6d73759 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -2,7 +2,7 @@ # 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 +# 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) @@ -12,14 +12,15 @@ 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.length.should eq 66 + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here - result.should have(2).items + 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 From 91c23b92c35a1d5cf46e49662f59a9e93ebb5f28 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Mon, 21 Oct 2013 20:20:54 -0700 Subject: [PATCH 2/7] Completed questions and test for week 2 --- week2/homework/questions.txt | 20 ++++++++++++++++++-- week2/homework/simon_says_spec.rb | 10 +++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..e7b638c 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -1,13 +1,29 @@ -Please Read The Chapters on: -Containers, Blocks, and Iterators +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? +While there are some simlilariies between an Array and a Hash, in that they are both indexed collections of object references, the main difference is that an Array is indexed using integers, while a Hash is indexed using objects of any type, for example strings or symbols. + 2. When would you use an Array over a Hash and vice versa? +You would use an Array over a Hash when you're unable, or do not know what type of object you will be using to index the contents. +You would use an Array over a Hash when you require an integer key to index the contents. +You would use a Hash over an Array when you need to use objects as indices. +You would use a Hash over an Array when it is important to keep track of the order in which the items were entered into the Hash. (Ruby returns them in the order in which they were received). + + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a way of grouping together methods, classes and contstants. They also provide a way to nampespace their contents, which is helpful in avoiding naming clashes. Modules are also usefule in that things that would not necessarialy form a class can be grouped together. + +Enumerable is a mixin module, which supplies the class that requires it to with a number of methods that allow the class to traverse, sort and search it's contents, as long as the class in question provides an each(iterator) method. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Ruby is a single-inheritance language, so a class has only one direct parent. However, a class in Ruby can include and use any number of mixins, which are like partial class definitions. So while a Ruby class has only one direct parent, it can behave like a class that supports multiple inheritance without the dangers that surround the kind of ambiguity that multiple inheritance can provide. + 5. What is the difference between a Module and a Class? + +A Module cannot have instances, as it is not a Class. A Module does not require instantiation, whereas a Class does. A Class has access to it's own intance variables, wheras a Module has access to any instance varibales of the Class it has been included in. diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..509dc2b 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -3,12 +3,12 @@ describe SimonSays do 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. it "should echo hello" do echo("hello").should == "hello" end - + it "should echo bye" do echo("bye").should == "bye" end @@ -16,13 +16,13 @@ it "should shout hello" do shout("hello").should == "HELLO" end - + it "should shout multiple words" do shout("hello world").should == "HELLO WORLD" end it "should repeat" do - repeat("hello").should == "hello hello" + repeat("hello", 2).should == "hello hello" end it "should repeat a number of times" do @@ -32,7 +32,7 @@ it "should return the first letter" do start_of_word("hello", 1).should == "h" end - + it "should return the first two letters" do start_of_word("Bob", 2).should == "Bo" end From db3d085bbb0ae591be9220377c844a8b6d253a1e Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Mon, 21 Oct 2013 20:25:35 -0700 Subject: [PATCH 3/7] Module to test against --- week2/homework/simon_says.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..34a8e09 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,22 @@ +module SimonSays + def echo(name) + return name + end + + def shout(name) + return name.upcase + end + + def repeat(name, n) + Array.new(n, name).join ' ' + end + + def start_of_word(name, n) + @frag = name.scan /\w/ + return @frag[0...n].join'' + end + + def first_word(name) + return name.split(" ")[0] + end +end \ No newline at end of file From c9ea8dbef40e49190e2a234cb892e930c33134c0 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Tue, 29 Oct 2013 11:37:22 -0700 Subject: [PATCH 4/7] Completed homework for week3 --- week3/homework/calculator.rb | 23 +++++++++++++++++++++++ week3/homework/questions.txt | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 week3/homework/calculator.rb diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..66e77b9 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,23 @@ +class Calculator + + def sum *values + values.flatten! + values.inject(0) { |val, i| val += i } + end + + def multiply *values + values.flatten! + values.inject(1) { |val, i| val *= i } + end + + def pow base, multiplier + values = Array.new multiplier, base + values.flatten! + values.inject(1) {|val, i| val *= i } + end + + def fac value + value == 0 ? 1 : value * fac(value-1) + end + +end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..d14e6dc 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -1,15 +1,36 @@ -Please Read: +Please Read: - Chapter 6 Standard Types - Review Blocks - Chapter 7 Regular Expressions - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A symbol is an identifier that you use to represent to a string or a name. 2. What is the difference between a symbol and a string? +Strings are immutable, while symbols are not. Also, symbols can accept interpolation, where a string literal cannot. 3. What is a block and how do I call a block? +A block is a set of statements and expressions that are wrapped in either braces or a do/end pair. The block immediately follows a method invocation, for example, after the .each method is calling a block here: + +[1,2,3,4].each do |value| + #block goes here +end 4. How do I pass a block to a method? What is the method signature? +You pass a block to a method by using the yield statement in a method that has the same name as the block. For example: +def test + yield +end +test{ puts "Hello world"} + +You can also pass the block into a method by passing it as the last argument of the method, and by prepending & to the block name. For example: + +def test(&block) + block.call +end +test { puts "Hello World!"} + 5. Where would you use regular expressions? +Anywhere that we would want to match, extract or changes a string based upon a given pattern. From 0b62619107e46ae728a241cfce165bf6c0c7f435 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Tue, 5 Nov 2013 11:51:56 -0800 Subject: [PATCH 5/7] Week 4 homework --- week4/exercises/worker.rb | 9 +++++++++ week4/homework/questions.txt | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 week4/exercises/worker.rb diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..a7ea493 --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,9 @@ +class Worker + + def self.work (repeat_count=1) + i = nil + repeat_count.times {i = yield} + i + end + +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..704e02c 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -1,9 +1,26 @@ -Please Read: +Please Read: Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + +Ruby reads files through I/0 -related methods that exist in the Kernel module. THese methods include, gets, open, print, printf, putc, puts, readline and test, which are all used to open and close files, as well as read the files' data and write to the file. + 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 + 3. What is the Directory class and what is it used for? + +The Dir class is used as a way or representing directory structures and their contents. This class allows us to manipulate the directory and file structure, search for, rename, delete and create files and directories. + + 4. What is an IO object? + +An IO object is a class that gives a bidirectional channel between a Ruby program and an external resource. The IO object can be written to and read from. + 5. What is rake and what is it used for? What is a rake task? + +Rake is used to build executable programs and libraries from source code. A rake task is an instruction in the form of code that is executed when running rake. A task may be grouped with other tasks, and contain dependencies to other files or tasks. From a8b2af4bbe89ee7be2011ab1646dba4c0978e9e3 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Tue, 26 Nov 2013 15:58:38 -0800 Subject: [PATCH 6/7] week7 homework --- .../features/step_definitions/pirate_steps.rb | 5 ++++- week7/homework/pirate_translator.rb | 10 ++++++++++ week7/homework/questions.txt | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 week7/homework/pirate_translator.rb diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..abd8e7a 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,6 @@ +# require '../../pirate_translator.rb' +require File.join(File.dirname(__FILE__), '..', '..', 'pirate_translator') + Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new end @@ -16,4 +19,4 @@ Letgoandhaul /^it also prints '(.+)'$/ do |arg| @result.split("\n ").last.should == arg -end +end \ No newline at end of file diff --git a/week7/homework/pirate_translator.rb b/week7/homework/pirate_translator.rb new file mode 100644 index 0000000..5f9d258 --- /dev/null +++ b/week7/homework/pirate_translator.rb @@ -0,0 +1,10 @@ +class PirateTranslator + def say(statement) + puts statement + end + + def translate(statement = '') + "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end + +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..2c5eb7a 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 used when and object is sent a message that it cannot find a method for. method_missing will override the defauly error that gets raised, and depending on the code you write within method_missing, handles the exception in a more graceful manner. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +An EignenClass is a metaclass of an object that is used to store class methods and singleton methods. When a Singleton method is created in an object, Ruby creates an Eigenclass class and stores the method there. + +Use duck typing when operating on collections containing objects from different classes. Duck typing helps you to reuse code, and reduces the amount of code you have to maintain. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +You can use ducktyping to accept different data types in your methods so that you have more reusable code, for example a method that can accept either and array or a hash as an argument means you don't have to check type, or write for different data types, resulting in longer, less maintainable code. + + 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 will run on the class object itself, wheras an instance method will run on the instance of a class object, and have access to the data of that specific instance. +class_eval allows you to evaluate code on the class object, where instance_eval allows you to evaluate code with an particular instance of a class. + 5. What is the difference between a singleton class and a singleton method? +When you add a method to a specific object Ruby inserts a new anonymous class into the inheritance hierarchy as a container to hold these types of methods. A singleton method is a method that is given to only a single object. From 7aa76a4a9047c28d297e7141e6aa5878480c6dd3 Mon Sep 17 00:00:00 2001 From: Dave Lane Date: Sun, 8 Dec 2013 19:09:12 -0800 Subject: [PATCH 7/7] Work to date for tic tac toe game --- .../step_definitions/tic-tac-toe-steps.rb | 3 + week7/homework/play_game.rb | 5 +- week7/homework/tic_tac_toe.rb | 192 ++++++++++++++++++ 3 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 week7/homework/tic_tac_toe.rb 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..7d9efc3 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,8 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' + +require File.join(File.dirname(__FILE__), '../', '../', 'tic_tac_toe.rb') + Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..d987854 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -1,4 +1,5 @@ -require './features/step_definitions/tic-tac-toe.rb' +#require './features/step_definitions/tic-tac-toe-steps.rb' +require './tic_tac_toe.rb' @game = TicTacToe.new puts "What is your name?" @@ -10,7 +11,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/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb new file mode 100644 index 0000000..f31f2aa --- /dev/null +++ b/week7/homework/tic_tac_toe.rb @@ -0,0 +1,192 @@ +class TicTacToe + + attr_accessor :player + attr_accessor :cpu + + def initialize player + @lines = [ + [:a1,:a2,:a3], + [:b1,:b2,:b3], + [:c1,:c2,:c3], + + [:a1,:b1,:c1], + [:a2,:b2,:c2], + [:a3,:b3,:c3], + + [:a1,:b2,:c3], + [:c1,:b2,:a3] + ] + + @places = { + a1:" ",a2:" ",a3:" ", + b1:" ",b2:" ",b3:" ", + c1:" ",c2:" ",c3:" " + } + + @game_over = false + + @player = player + + puts "TIC TAC TOE" + + end + + def welcome_player + draw_line + @player_name = @player + @cpu_name = "Computer" + + @cpu = rand() > 0.5 ? 'X' : 'O' + @player = @cpu == 'X' ? 'O' : 'X' + + draw_line + start_game + + "Welcome #{@player_name}" + end + + def start_game + player_turn + end + + def draw_game + puts "" + puts " #{@player_name}: #{@player}" + puts " #{@cpu_name}: #{@cpu}" + puts "" + puts " a b c" + puts "" + puts " 1 #{@places[:a1]} | #{@places[:b1]} | #{@places[:c1]} " + puts " --- --- ---" + puts " 2 #{@places[:a2]} | #{@places[:b2]} | #{@places[:c2]} " + puts " --- --- ---" + puts " 3 #{@places[:a3]} | #{@places[:b3]} | #{@places[:c3]} " + puts "" + end + + def cpu_turn + move = cpu_find_move + @places[move] = @cpu + draw_line + check_game(@player) + end + + def cpu_find_move + + + k = @places.keys; + i = rand(k.length) + if @places[k[i]] == " " + return k[i] + else + @places.each { |k,v| return k if v == " " } + end + end + + def times_in_column arr, item + times = 0 + arr.each do |i| + times += 1 if @places[i] == item + unless @places[i] == item || @places[i] == " " + return 0 + end + end + times + end + + def empty_in_column arr + arr.each do |i| + if @places[i] == " " + return i + end + end + end + + def player_turn + draw_game + print " #{@player_name}, please input move coordinates move or type 'exit' to quit: " + input = gets.chomp.downcase.to_sym + if input.length == 2 + a = input.to_s.split("") + if(['a','b','c'].include? a[0]) + if(['1','2','3'].include? a[1]) + if @places[input] == " " + @places[input] = @player + check_game(@cpu) + else + wrong_move + end + else + wrong_input + end + else + wrong_input + end + else + wrong_input unless input == :exit + end + end + + def wrong_input + draw_line + puts " Invalud coordinates." + player_turn + end + + def wrong_move + draw_line + puts " You must choose an empty slot" + player_turn + end + + def moves_left + @places.values.select{ |v| v == " " }.length + end + + + def check_game(next_turn) + + game_over = nil + + @lines.each do |column| + if times_in_column(column, @cpu) == 3 + draw_line + draw_game + draw_line + puts "" + puts " Game Over -- #{@cpu_name} WINS!!!" + game_over = true + end + if times_in_column(column, @player) == 3 + draw_line + draw_game + draw_line + puts "" + puts " Game Over -- #{@player_name} WINS!!!" + game_over = true + + end + end + + unless game_over + if(moves_left > 0) + if(next_turn == @player) + player_turn + else + cpu_turn + end + else + draw_line + draw_game + draw_line + puts "" + puts " Game Over -- DRAW!\n" + end + end + end + + def draw_line + puts ("-" * 80) + end + +end \ No newline at end of file