diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..69faaa9 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -2,14 +2,20 @@ Please read: Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) -1. What is an object? +1. What is an object? +A: Basically, everything you manipulate in Ruby is an object, and the results of these manipulations are objects as well. -2. What is a variable? +2. What is a variable? +A: Variables are used to keep track of objects. They provide a reference to an object. -3. What is the difference between an object and a class? +3. What is the difference between an object and a class? +A: Objects begin as instances of classes. A class is a collection of characteristics and functionality and serves as a blueprint from which an object is created. -4. What is a String? +4. What is a String? +A: In Ruby, strings are sequences of characters. They are objects of the class String. 5. What are three messages that I can send to a string object? Hint: think methods +A: Three common messages are scan, split, and squeeze. 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +A: A String literal can be defined by single quotes and by double quotes. The use of double quotes provides many more options for escape sequences and allows you to substitute the value of Ruby code into the string. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..8730fec 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -9,18 +9,22 @@ describe String do context "When a string is defined" do - before(:all) 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 split on the . charater" do - pending - result = #do something with @my_string here + + it "should be able to count the characters" do + expect(@my_string.length).to eq(66) + end + + it "should be able to split on the . character" do + 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"))' +# pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + (@my_string.encoding).should eq (Encoding.find("UTF-8")) end end -end - +end \ No newline at end of file diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..dc8a83c 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -2,12 +2,23 @@ Please Read The Chapters on: Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins +Submitted by: Doug MacDowell + 1. What is the difference between a Hash and an Array? +A. Both hashes and arrays are similar in that they hold a collection of object references. The difference is in how the object references are identified. In arrays, the position of the object reference is identified by a non-negative integer index, while in a hash, the object reference can be indexed with objects of any type, such as symbols or strings, etc. + 2. When would you use an Array over a Hash and vice versa? +A. An array is more suited for storing items that do not need to be returned in a specific order. With hashes, Ruby remembers the order in which items are added to the hash, and when iterating over these items, Ruby generally returns them in order. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A. A module is a collection of methods and constants, as well as classes. The Enumerable module is a mixin that provides collection classes with methods that can traverse, search, and sort the members of the collection. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +A. Yes. To get around this, we should design using compositional relationships, rather than strict, tightly-coupled hierarchies. + 5. What is the difference between a Module and a Class? +A. A module is basically a class that cannot be instantiated. And unlike a class, a module can be used to group items together that don't naturally form a class, by the use of a namespace to segregate diverse methods and constants. A class inherits behavior, while there is no inheritance in modules. Modules can provide functionality across multiple classes, whereas a class is more object-centric. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..0bc9ec0 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,31 @@ +module SimonSays + + attr_reader :the_string + + def initialize + @the_string = the_string + end + + def echo the_string + the_string + end + + def shout(the_string) + the_string.upcase + end + + def repeat(the_string, the_count=2) + a = Array.new(the_count, the_string) + a.join ' ' + end + + def start_of_word(the_string, n) + letters = n - 1 + the_string.slice(0..letters) + end + + def first_word(the_string) + the_string.split[0..0].join(' ') + end + +end diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..2f18c0f --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,24 @@ +class Calculator + + def pow(val1, val2) + val1**val2 + end + + def sum(the_input) + the_sum = 0 + the_input.each do |value| + the_sum += value + end + return the_sum + end + + def fac(x) + (1..(x.zero? ? 1 : x)).inject(:*) + end + +# the version below works for multiple values and arrays + def multiply(*input) + input.flatten.inject(:*) + end + +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..54c0ee5 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,24 @@ Please Read: 1. What is a symbol? +A. A symbol is an identifier corresponding to a string of characters, often a name. Symbol objects are constructed using by preceding the name with a colon. Arbitrary strings can be symbolized by preceding the string literal with a colon. + 2. What is the difference between a symbol and a string? +A. A symbol is actually a string, but the difference is that a symbol is immutable. That is, unlike a string, a symbol can be overwritten but not changed. The receiver methods we can use on strings won't work with a symbol. + 3. What is a block and how do I call a block? +A. A block is basically a chunk of Ruby code that can be associated with method invocations. The code in the block is not executed as it is encountered, but Ruby instead remembers the context in which the block appears and then enters the associated method. Blocks consist of code that runs under some sort of transactional control. + +A block is called when associated with a method. The content of a block can be placed between braces, or between a "do" and "end". A block may also be invoked within a method by using the "yield" statement. + 4. How do I pass a block to a method? What is the method signature? +A. When executing a method, you can add a block of code by surrounding the block in braces. An iterator method can also target and invoke a block. + +A method signature defines the number and order in which arguments are to be passed into the method. + 5. Where would you use regular expressions? + +A. Regular expressions are useful when working with strings. There are times when the Ruby String class does not support what is needed in practice, such as testing for pattern matches, searching and replacing specific pieces of a string, or extracting particular parts of a string. These are tasks that regular expressions are designed for. \ No newline at end of file diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..4622a9e --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,15 @@ +class Worker + +# this only works for one block execution +# +# def self.work +# yield +# end +# +# this works for all blocks in this exercise +# + def self.work(count = 1) + (0..count).inject {yield} + end + +end diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..a2e4de0 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,20 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +A. Ruby has a base class called IO that is subclassed by the File class. IO objects are able to be read (and written) between Ruby and the file. + 2. How would you output "Hello World!" to a file called my_output.txt? +A. 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. Objects in this class are directory streams that represent the directories of a file system. This class is used mainly to list the directories and their content. + 4. What is an IO object? +A. An IO object is a bidirectional communications channel between a Ruby program and some external resource, such as a file. + 5. What is rake and what is it used for? What is a rake task? +A. Rake is a Ruby build program, similar to "make". It is used for building rakefiles using Ruby syntax, and has a library of prepackaged tasks that perform operations like building tarballs or publishing to FTP. + +A rake task is a basic unit of work in a rakefile. Rake tasks have both actions and a list of prerequisites to check before taking an action. \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..46ddad0 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,11 @@ +class PirateTranslator + + def initialize + @reply = "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + end + + def send(*arg) + @result = @reply + end + +end 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..118625e --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,87 @@ +class TicTacToe + + include Enumerable + + SYMBOLS = [:X, :O] + + attr_accessor :player, :computer, :board, :X, :O, :old_pos + + def initialize(player=nil, computer=nil, old_pos=nil, com_move=nil) + @player = player + @computer = computer + @old_pos = old_pos + @com_move = com_move + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def welcome_player + puts "The PLAYER in the Welcome method is #{@player}" + "Welcome #{@player}" + end + + def current_player + puts "The current PLAYER is #{@player}" +# @player = "Renee" + [@player, "Computer"].sample +# [@player, @computer].sample + end + + def player_symbol + @player = [:X, :O].sample +# puts "The SYMBOL chosen for player is #{@player}" + end + + def computer_symbol + if @player.equal? :X + @computer = :O + else + @computer = :X + end + end + + def with + end + + def indicate_palyer_turn + end + + def and_return + end + + def get_player_move + end + + def board(arg1) + + end + + def open_spots + end + + def player_move + + end + + def computer_move + end + + def current_state + end + + def determine_winner + + end + + def draw? + + end + + def player_won? + + end + +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/tictactoe.rb b/week7/homework/features/step_definitions/tictactoe.rb new file mode 100644 index 0000000..931d7d7 --- /dev/null +++ b/week7/homework/features/step_definitions/tictactoe.rb @@ -0,0 +1,71 @@ +class TicTacToe + + include Enumerable + + SYMBOLS = [:X, :O] + + attr_accessor :player, :computer, :board, :X, :O, :old_pos + + def initialize(player=nil, computer=nil, old_pos=nil, com_move=nil) + @player = player + @computer = computer + @old_pos = old_pos + @com_move = com_move + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def welcome_player + puts "The PLAYER in the Welcome method is #{@player}" + "Welcome #{@player}" + end + + def current_player + puts "The current PLAYER is #{@player}" +# @player = "Renee" + [@player, "Computer"].sample +# [@player, @computer].sample + end + + def player_symbol + @player = [:X, :O].sample +# puts "The SYMBOL chosen for player is #{@player}" + end + + def computer_symbol + if @player.equal? :X + @computer = :O + else + @computer = :X + end + end + + def with(arg1) + end + + def indicate_palyer_turn + end + + def and_return(arg1) + end + + def get_player_move + end + + def board(arg1) + + end + + def open_spots + end + + def computer_move + end + + def current_state + end + +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..a9715ca 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,16 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +A. This is a hook method that Ruby will try to invoke on an object when a method call is made and Ruby can't find the method in the object's class or superclasses. It can be used to intercept and handle (or report) undefined methods, or it can be used with patterns to selectively forward particular method calls to a superclass. + 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 holds an object's singleton methods. It is used when you need Ruby to find the object's singleton methods without disrupting the method lookup path. Singleton methods are defined on the objects themselves, rather than in its class, therefore they are dynamic. + 3. When would you use DuckTypeing? How would you use it to improve your code? +A. Duck Typing is a useful technique in situations where you want flexibility, without having to check the type of the arguments, which generally doesn't add any value. Code is improved and more flexible when you don't have to include checks on the argument types. + 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 defined within a class and the method can be used in any subclasses of that class. Instance methods are created in a module and are available as instance methods of the class that includes the module. class_eval sets things up as if you were within a class definition. Any method definitions created there define instance methods. Calling instance_eval on a class differs because method definitions created there are class methods. + 5. What is the difference between a singleton class and a singleton method? +A. A singleton method is a method that is specific to a particular object, while a singleton class is an anonymous class that Ruby creates when defining a singleton method. \ No newline at end of file