From 7329c8822d9088ddbc77d6bc286ac1ff407243b9 Mon Sep 17 00:00:00 2001 From: Remy Date: Mon, 21 Oct 2013 23:51:59 -0700 Subject: [PATCH 1/5] Week 2 homework for upload --- week2/exercises/mad_libs.rb | 17 ++++++++++++++++- week2/homework/questions.txt | 5 +++++ week2/homework/simon_says.rb | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 3af5583..9305d36 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -1,3 +1,18 @@ +puts "Enter a present tense verb" +verb = gets.chomp +puts "Enter a formal noun" +name = gets.chomp +puts "Enter an adverb" +adverb = gets.chomp +puts "Enter an adjective" +adjective = gets.chomp +puts "Enter a common noun" +noun = gets.chomp +story = "#{name} #{verb} the #{adjective} #{noun} #{adverb}" +puts story +puts + + puts "Please enter a noun" noun = gets.chomp puts "Please enter an adjective" @@ -7,4 +22,4 @@ puts "What does the #{noun} say?" says = gets.chomp story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" -puts story +puts story \ No newline at end of file diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..2f0af0d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + 1a. An array is an ordered collection of objects, referenceable by its sequential position in the array, or just by looking for it or some characteristic of it. A hash is an unordered collection of objects, referenceable by a key that refers to the value following the key. In a hash, you use the key to 'search' for the value of the key. 2. When would you use an Array over a Hash and vice versa? + 2a. An array is a very efficient way to handle collections of objects that exist without reference to any other object. A hash is more useful when objects have another piece of information they relate to or are the values of, and that information is useful for identifying the related value. 3. What is a module? Enumerable is a built in Ruby module, what is it? + 3a. A module is a block of code that starts with the word 'module,' followed by the name of the module and ends with a matching 'end.' A module is way of grouping methods, classes and constants and defines a controllable namespace that can be called unambigously by defining referencable objects using the name of the module followed by a period followed by the name of the referenceable object. Using the enter module.object combination, the names can be referenced unambiguously. Enumerable, as a standard mixin, is a module that doesn't need to be explicity included, and it brings a host of operations for dealing with collections of objects, including sorting, finding and testing conditions. 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + 4a. An instance of a class can inherit the attributes and functionality of its parent class, but it can't inherit from multiple parents. This can be gotten around by using mixins, which reference modules with attributes and functionalities different from the parent class. 5. What is the difference between a Module and a Class? + 5a. While both are constructed in Ruby code, and many of both exist in the standard Ruby library, a module is a general collection of code, and can include classes and other namespaced code blocks. A class is a generalize description of a type (class) of object, of which there will be many specific instances. Classes can also have subclasses (children) that inherit the attributes and functionality of the parent class. Modules are not designed to have instances that inherit. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..793253b --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,24 @@ +module SimonSays + + def echo(greeting) + greeting + end + + def shout(greeting) + greeting.upcase + end + + def repeat(greeting, how_many=2) + long_greeting = ((greeting + " ") * how_many).rstrip + end + + def start_of_word(word, range) + word[0..(range - 1)] + end + + def first_word(phrase) + phrase[0..(phrase.index(" ") - 1 )] + end + +end + From 08c48299646db9d0182967b25e50db53c882568c Mon Sep 17 00:00:00 2001 From: Remy Date: Wed, 23 Oct 2013 19:17:35 -0700 Subject: [PATCH 2/5] Posting post-class revisions --- week2/exercises/book.rb | 23 +++++++++++++++++------ week2/exercises/book_spec.rb | 16 ++++++++++++---- week2/homework/simon_says.rb | 16 ++++++++-------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 50bc054..a67efe1 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,13 +1,24 @@ class Book - attr_accessor :title, :pages + attr_accessor :title, :page_count - def initialize(title, pages) + @@book_count = 0 # sets initial value for class + + def self.book_count + @@book_count # cless level + end + + def initialize title = "Not Set" page_count = 0 + @@book_count += 1 + @page_count = page_count @title = title - @pages = pages end - def page_count - "Page count is #{@pages}" + def test + @test = "Hello" end -end + + def output_test + puts @test + +end \ No newline at end of file diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index bb22819..13fb5f5 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,14 +1,22 @@ require './book.rb' describe Book do + + context "::book_count" do + it "should count how many books have been created" + end + before :each do - @book = Book.new("Harry Potter", 200) + @book = Book.new end + it "should respond to title" do @book.should respond_to "title" end - it "should return the page count" do - @book.page_count.should eq "Page count is 200" + 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/simon_says.rb b/week2/homework/simon_says.rb index 793253b..89bc399 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,23 +1,23 @@ module SimonSays - def echo(greeting) + def echo greeting greeting end - def shout(greeting) + def shout greeting greeting.upcase end - def repeat(greeting, how_many=2) - long_greeting = ((greeting + " ") * how_many).rstrip + def repeat greeting, how_many=2 + long_greeting = ((greeting + " ") * how_many).chop end - def start_of_word(word, range) - word[0..(range - 1)] + def start_of_word word, range + word[0,range] end - def first_word(phrase) - phrase[0..(phrase.index(" ") - 1 )] + def first_word phrase + phrase.split.first end end From a91b1eeb3d8ff0411f183fd5f80ff42d38dabe9a Mon Sep 17 00:00:00 2001 From: Remy Date: Mon, 28 Oct 2013 23:08:44 -0700 Subject: [PATCH 3/5] Upload Week 3 homework --- week3/homework/calculator.rb | 29 +++++++++++++++++++++++++++++ week3/homework/calculator_spec.rb | 3 ++- week3/homework/questions.txt | 5 +++++ 3 files changed, 36 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..f78bdd3 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,29 @@ +class Calculator + + def sum arr + sum = 0 + arr.each { |a| sum += a } + sum + end + + def multiply arr, opt=0 + product = 1 + if arr.kind_of? Array + arr.each { |a| product *= a } + product + else + product = arr * opt + end + end + + def fac n + product = 1 + 1.upto(n) { |a| product = product * a } + product + end + + def pow base, power + expo = base**power + end + +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..2304bf6 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,4 +1,5 @@ -require "#{File.dirname(__FILE__)}/calculator" +# require "#{File.dirname(__FILE__)}/calculator" +require "./calculator.rb" describe Calculator do diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..8be38fe 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,16 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + 1a. A symbol, created and indicated by a preceding colon (e.g., :name) is the identifier of a string (e.g., name). What the string references (its value) can change, but the symbol doesn't change its value, and always returns the colon followed by the string name (e.g. in this example, it's value is always :name, even though the value of name may be "Pat" or change to something else). 2. What is the difference between a symbol and a string? + 2a. A symbol is an identifier of a string, the string possibly being a variable name; in that case it's the name of the name, "the thing called :thing". It is created by putting a colon at the end of the string to create its symbol. The "value" of the symbol (what's returned when you show it, is the name (not the value) of the string it references. A string is an object that is a series of characters, usually identified by single or double quotes around it. It can be referenced by a variable, and that variable can, with the addition of a preceding colon, have a symbol that identifies it. 3. What is a block and how do I call a block? + 3a. A block is a collection of code that either starts with "do" and ends with "end,"" or starts with an open brace "{" and ends with a closing brace "}". If a block starts with "def" it defines a named method consisting of that block, and it's called as a method by appending it to an object after a period ("."), if the method name is available in the range where the call is made, which can be done by "require"-ing the file it's in, then "include"-ing the name of the method or its parent module name. 4. How do I pass a block to a method? What is the method signature? + 4a. You pass a block to a method by specifying 'def' followed by the name of the method, followed by the code that specifies the method, followed by 'end' to indicate the end of the method. The code between 'def' and 'end' (or for shorter coding, usually an opening { and a closing } ), is the block. When the name of the method is invoked to act on an object ("called"), it passes the block to the location of the call to act on the object according to the code in the block. This call may include passing some parameters for the method to use. The value returned to the method is the last value rendered before 'end' unless other steps are taken to return other values. The method signature is the name of the method plus any arguments (parameters) that are passed to the method's block from the calling invocation of the method name. 5. Where would you use regular expressions? + 5a. You would use a regular expression to identify whether (or not), and where a particular string or string pattern occurs within another string. Once identified, the first occurrence of that string can be changed (with the method ".sub" to "sub"stitute another string), either returning a new string that has the change, or changed within the original string (with the method ".sub!" to force the substitution into the original string). If you want to change all occurrences of the string use the ".gsub"("global sub") method to generate a new string, or ".gsub!" to change the original string. \ No newline at end of file From 38ad48c729efc6bdbb4f575874d8dddb779aa37a Mon Sep 17 00:00:00 2001 From: Remy Date: Tue, 5 Nov 2013 17:55:03 -0800 Subject: [PATCH 4/5] Pat Remy updated worker.rb; move to class approach from module --- week4/homework/worker.rb | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb index 2061b91..9cdc691 100644 --- a/week4/homework/worker.rb +++ b/week4/homework/worker.rb @@ -1,21 +1,22 @@ -module Worker +#@module Worker +class Worker -#=begin trivial case +=begin trivial case def work no_of_times=1 yield yield yield end -#=end +=end -=begin named block, iterative case FAIL ==> returns value of no_of_times in every test - def self.work no_of_times=1, &block_given - no_of_times.times do - block_given.call - end +#=begin named block, iterative case FAIL ==> returns value of no_of_times in every test + def self.work no_of_times=1 + result = "" + no_of_times.times { result = yield if block_given? } + result end -=end +#=end =begin named block, trivial case def self.work no_of_times=1 @@ -24,10 +25,8 @@ def self.work no_of_times=1 =end =begin named block, trivial case - def self.work no_of_times=1, &block_given - block_given.call - block_given.call - block_given.call + def self.work no_of_times=1 + 1.upto(no_of_times) { yield } end =end From d694ebae09fc5c87c14e1a82448f59bb003826f1 Mon Sep 17 00:00:00 2001 From: Remy Date: Mon, 25 Nov 2013 20:40:29 -0800 Subject: [PATCH 5/5] week 7 homework upload --- .../homework/features/step_definitions/pirate.rb | 13 +++++++++++++ .../features/step_definitions/pirate_steps.rb | 3 ++- week7/homework/questions.txt | 15 +++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 week7/homework/features/step_definitions/pirate.rb diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..e40491a --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,13 @@ +class PirateTranslator + + def say arg1 + @english = arg1 + end + + def translate + @result = "" + datastore = { 'Hello Friend' => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"} + @result = datastore[@english] + end + +end # of class PirateTranslator \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..832b2a4 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,5 +1,6 @@ Gangway /^I have a (\w+)$/ do |arg| - @translator = Kernel.const_get(arg).new +# @translator = Kernel.const_get(arg).new + @translator = PirateTranslator.new end Blimey /^I (\w+) '(.+)'$/ do |method, arg| diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..5c68b77 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,18 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? -2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? -3. When would you use DuckTypeing? How would you use it to improve your code? + 1a. method_missing is a hook method called by the Ruby interpreter when a called method is not defined. That missing method can be defined by us to handle the method_missing condition with something useful to us, such as delivering a clearer diagnostic message or defining what the method should be in the absence of a definition. The method_missing definition substitutes itself as the (previously undefined) method that was called. This definition and substitution can be made conditional, but failures to all conditions should lead to an explicit call to "super" to forward the call to superclasses so that the failure to find the called method results in an error message rather than being silently ignored. method_missing can also be used to create customized methods on the fly using conditions (such as pattern recognition with Regexp's) that analyze incoming arguments to build or choose the method that becomes the called method. + +2. What is an Eigenclass and what is it used for? Where Do Singleton methods live? + 2a. An eigenclass--also known as a singleton class--is an unnamed class created on the fly when a method is defined for an object and that method doesn't exist in the object's class or any of its superclasses. The singleton methods live in this anonymous (unnamed) class. This singleton class becomes the object's immediate class, placing itself between the object and what would have been--in the absence of these method definitions--the object's class, which then becomes the object's superclass. + +3. When would you use DuckTyping? How would you use it to improve your code? + 3a. You'd use DuckTyping when you want to maintain modularity and flexibility in what's being done with some object, since DuckTyping enables behavior based on what methods objects can respond to, and different classes of objects can respond to the same methods. An obvious example is the << (append) method, which can work on files, strings and arrays. DuckTyping could be used to improve code by enabling this flexibility and abstraction about the objects to which methods are passed, enabling modular adaptations for code testing or reusing code for structurally different (in terms of the objects manipulated) but operationally similar (in terms of the methods used) needs. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + 4a. Class methods are methods called on a class and instance methods are called on an instance of a class. Class methods are commonly identified in the class definitions with the "self." prefixed, and can be called with the class name without creating a new instance of the class. Instance methods don't have the ".self" prefix in their definition, and can only be called after an instance of the class is created, and the call to the method in the class must be made with the instance created. + + instance_eval sets "self" to an object and evaluates code in a block, creating singleton methods in the object's singleton class. class_eval sets "self" to an object and evaluates code in a block, creating instance methods as if it were working within the singleton class. + 5. What is the difference between a singleton class and a singleton method? + 5a. A singleton class is created by a singleton method. The singleton class "houses" the method that created it, so it can be called within the bounds of its closure. The singleton method is defined by the coder; the singleton class is created by the Ruby interpreter. \ No newline at end of file