diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..7e35e8b 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,23 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +A instance or 'thing' of any type that is defined in memory within the context of the program 2. What is a variable? +A name that is given to a specific object 3. What is the difference between an object and a class? +An object is a specific instance of a class. A class may have specific attributes that are themselves objects of another class. 4. What is a String? +A string is one or more characters. A string object is an object that is a member of the string class. 5. What are three messages that I can send to a string object? Hint: think methods +length (return length of string) +index (return first character location of a hit on a substring or regex pattern) +encode (convert character encoding) 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +single (') and double (") quotes... +single quotes don't interpolate variables and only require escaping the single-quote(') or backslash(\) +double quotes allow variable interpolation and have many more 'special' characters that either require escaping or that can be created through the use of backslash(\) \ 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..fc0bfba 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -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.size.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"))' + @my_string.encoding.should eq Encoding.find("UTF-8") end end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..166210f 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,24 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array is a collection of objects that are indexed by their order in memory and will therefore be returned in a predictable order +A hash is a collection of objects indexed by an arbitrary hash key. Items may be returned in an unpredictable order 2. When would you use an Array over a Hash and vice versa? +An array is ideal for creating a push/pop stack and anytime an integer indexed key makes logical sense. +Arrays are also generally more memory efficient than hashes, so anytime memory limitations are a concern. +Otherwise, hashes are generally more flexible and lookup speeds are generally faster. 3. What is a module? Enumerable is a built in Ruby module, what is it? +Modules act as libraries and are a way to create methods/functions that will be used in multiple classes. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No, Ruby does not have multiple inheritance. +To get around the problem 'include' can be used to mixin multiple modules into a single class. 5. What is the difference between a Module and a Class? +Generally, a module is about methods and a class is about objects... +Objects can be instances of a class, modules cannot be instantiated. +Class can inherit from other classes but cannot be 'included' in a class. +Modules have no pure inheritance, but can be 'included' in other classes and modules (using include) \ 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..cee7881 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + def echo(input) + input + end + + def shout(input) + input.upcase + end + + def repeat(input, times=2) + ((input + ' ') * times).chop + end + + def start_of_word(input,last) + input[0...last] + end + + def first_word(input) + input.split(' ')[0] + end +end \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..a7e8768 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,19 @@ +class Calculator + + def sum (input) + input.inject(0,:+) + end + + def multiply (*input) + input.flatten.inject(:*) + end + + def pow(base,exp) + (1...exp).to_a.inject(base){|result| result*=base} + end + + def fac(n) + (1..n).to_a.inject(1){|result,i| result*=i} + end + +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..0f90902 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,25 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A symbol is a unique, fixed static name 2. What is the difference between a symbol and a string? +A string is an object and the character contents of that object can be changed at any time. +Multiple string object instances can contain the same string value. +A symbol is a collection of characters (like a string), but the name and value of a symbol are the same, they cannot be changed, and are always unique. +Every reference to a specific symbol points to the same memory object while individual references to a specific string value point to individual in-memory string object instances. 3. What is a block and how do I call a block? +A block is an unnamed function or section of code which can be called with the yield function. 4. How do I pass a block to a method? What is the method signature? +A block is delineated by a pair of curly-braces {} or by do/end keywords. +A block is passed to a method by placing the block immediately after the method call. All methods receive blocks, which can be called with yield. +A method signature is part of the method declaration. It is the combination of the method name and the parameter list. +Blocks can be defined in the method signature by using the ampersand & prefix and then accessed within the method by using the 'call' method. +Performance is generally better using 'yield' vs 'call' so method signature block declaration is generally only used when necessary. 5. Where would you use regular expressions? +Regexes are used to match (and/or replace/extract) potentially complex pattern conditions within text. +Regexes are generally less efficient than simpler equality matches, and should thus be reserved for situations where they are required. + diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..e929afc 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,18 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Files are read using the Ruby built-in File class which is a subclass of the IO class + 2. How would you output "Hello World!" to a file called my_output.txt? +File.open('my_output.txt', 'w+') {|fileout| fileout.puts "Hello World!"} + 3. What is the Directory class and what is it used for? +Objects of class Dir are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. + 4. What is an IO object? +An IO object is any instance of an IO class. Since the File class is a subclass of IO, all File objects are IO objects. + 5. What is rake and what is it used for? What is a rake task? +rake = ruby make +It is a build tool to manage dependencies. They are used wherever make files would normally be used but use ruby syntax instead of make syntax +A rake task is a logical group of ruby statements. Tasks can have prerequisites and depend on other tasks. In this manner, tasks can be used to group other tasks into sequences. 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..0895449 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,108 @@ +class TicTacToe + attr_accessor :board, :player, :player_symbol, :computer_symbol + + @WINNING_COMBOS=[[:A1, :A2, :A3], [:B1, :B2, :B3], [:C1, :C2, :C3], [:A1, :B1, :C1], [:A2, :B2, :C2], [:A3, :B3, :C3], [:A1, :B2, :C3], [:A3, :B2, :C1]] + + def initialize (player=[:player, :computer].sample, symbol=[:X,:O].sample) + @player = '' + @board = { + :A1 => :' ', :A2 => :' ', :A3 => :' ', + :B1 => :' ', :B2 => :' ', :B3 => :' ', + :C1 => :' ', :C2 => :' ', :C3 => :' ' + } + @winner = nil + @next_turn = player + if symbol == :X + other_symbol=:O + else + other symbol = :X + end + if player == :player + @player_symbol = symbol + @computer_symbol = other_symbol + else + @player_symbol = other_symbol + @computer_symbol = symbol + end + end + + def current_player + if @next_turn == :computer + return 'Computer' + else + @player + end + end + + def welcome_player + puts "Welcome #{@player}" + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def open_spots + @board.select {|k,v| v==:' '}.keys + end + + def spots_open? + if self.open_spots.length > 0 + true + else + false + end + end + + def get_player_move + player_move = gets.chomp + until self.open_spots.include?(player_move) + puts "That spot is taken." + self.indicate_palyer_turn + player_move=gets.chomp + end + @board[player_move => @player_symbol] + @next_turn = :computer + end + + def computer_move + @board[self.open_spots.sample => @computer_symbol] + @next_turn = :player + end + + def determine_winner + @winner = :player if board.select {|k,v| v==@player_symbol}.keys.permutation(3).any? {|combo| @WINNING_COMBOS.include?(combo)} + @winner = :computer if board.select {|k,v| v==@player_symbol}.keys.permutation(3).any? {|combo| @WINNING_COMBOS.include?(combo)} + end + + def player_won? + if @winner == :player + true + else false + end + end + + def computer_won? + if @winner == :computer + true + else false + end + end + + def over? + if self.spots_open? == false or @winner + true + else + false + end + end + + def draw? + if @winner.nil? and self.over? + true + else + false + end + end + +end \ No newline at end of file