diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..421a0a5 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,22 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? + 1a. An object is anything that can be manipulated by another operation,and the results of those manipulations are also objects. They usually begin as instances of Classes, which 2. What is a variable? + 2a. A variable is a collection of lettters and/or numbers, starting with a lower case letter (or for an instance variable, a @, or a global variable a $), to which value is assigned by putting the variable on the left followed by a single equal sign followed on the right by the value being placed into the variable. That value can be changed. 3. What is the difference between an object and a class? + 3a. A class is the general description of something (a real world thing or a concept with characteristics) and an object is an instance--a specific case--of that general class. Objects can also be created from other objects. An object is something that can be manipulated by code. 4. What is a String? + 4a. A string is an ordered collection of usually but not always printable individual characters in whatever encoding is present when they're put together. Simplisticaly and usually, a string is a series of letters and numbers. They can be typed in final form from a keyboard or assembled on the fly with code. 5. What are three messages that I can send to a string object? Hint: think methods + 5a. (1) .length returns the number of characters in the string; (2) .capitalize returns a string that copies the string and changes it so the first letter of the first word is capitalized and all other letters are lower case; (3) .capitalize! changes the original string stored in a variable name to capitalize the first letter of the first word and convert all other letters are lower case; if changes are made, it returns the changed string; if no changes are made, it returns nil. 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + + 6a. Single quotes and double quotes surrounding what, as a result of the presence of those quotes, is defined as the string literal. + + 6bonus. Single quotes allow for limited internal interpolation (substitution of processed information), identified so far as only an escape (backslash) before the single quote (so it doesn't end the literal) and an escape (backslash) before another backslash, so you can treat the backslash as a printable text character. Double quotes allow for a broader range of '\'-initiated escape sequences (like a new line feed, \n), as well as code-interpreted renderings of information, such as the values of variables, calculations, expressions and (presumably) anything that can be handled as an object. These interpolations--renderings of code--occur inside the '#{' opener and '}' closer. \ No newline at end of file diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index a6b943d..7bad069 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -2,6 +2,28 @@ class Book attr_accessor :title attr_reader :page_count +<<<<<<< HEAD + attr_accessor :title, :page_count + + @@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 + end + + def test + @test = "Hello" + end + + def output_test + puts @test +======= @@book_count = 0 def self.book_count @@ -23,5 +45,6 @@ def out_put_test puts @test puts @@book_count end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 end \ No newline at end of file diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..c3e7398 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -2,6 +2,24 @@ describe Book do +<<<<<<< HEAD + context "::book_count" do + it "should count how many books have been created" + end + + before :each do + @book = Book.new + end + + it "should respond to title" do + @book.should respond_to "title" + end + + it "should allow me to set the title" do + @book.title = "Snow Crash" + @book.title.should eq "Snow Crash" + end +======= context "::book_count" do @@ -45,5 +63,6 @@ end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 end \ No newline at end of file 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..89bc399 --- /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).chop + end + + def start_of_word word, range + word[0,range] + end + + def first_word phrase + phrase.split.first + end + +end + diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..fe85865 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,44 @@ +class Calculator +#---------------------------------------------------------- + def sum arr +# sum = 0 # my original +# arr.each { |a| sum += a } # my original +# sum # my original + # arr.inject(0){|sum,a| sum += a } #alt 1 + arr.inject( 0, :+ ) #alt 2 + end +#---------------------------------------------------------- + def multiply arr, opt=0 + #def multiply *arr #alt 1 splat args makes them optionsl # alt + product = 1 # my original + if arr.kind_of? Array + arr.each { |a| product *= a } # my original + product # my original + else # my original + product = arr * opt # my original + end # my original + + + # arr.flatten.inject(:*) #alt + + end +#---------------------------------------------------------- + def fac n + # product = 1 # my original = alt 1 + # 1.upto(n) { |a| product = product * a } # my original + # 1.upto(n) { |a| product *= a } #alt 1 + # product # my original = alt 1 + + (1..n).to_a.inject(1,:*) #alt 2 + + # return 1 if n==0 #alt3 alt a + # return 1 if n.zero? #alt3 alt b + # n * fac(n-1) #alt3 recursive; calls itself + end +#---------------------------------------------------------- + def pow base, power + # expo = base**power # my original + base**power #alt + 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 diff --git a/week4/class_materials/odd_number.rb b/week4/class_materials/odd_number.rb index d41cf85..917fd7e 100644 --- a/week4/class_materials/odd_number.rb +++ b/week4/class_materials/odd_number.rb @@ -15,8 +15,8 @@ def succ new_val end - def <=> (other) - @value <=> other.value + def <=> (other_odd_number) + @value <=> other_.value end end diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..fa27a41 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -1,9 +1,24 @@ Please Read: -Chapter 10 Basic Input and Output +Chapter 11 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + 1a. Ruby reads files by invoking the open method on an object of the File class; e.g. File.open(filename), then doing some operation that does something with what's there, such as file.gets, which reads a line from the file (the string up to a CR or other defined line ending. + 2. How would you output "Hello World!" to a file called my_output.txt? + 2a. I would (and did to see if it worked) write and test the following code in .rb file: + + 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? + 3a. The Directory class is used to instantiate objects that contain information about the contents of the file system. It's used to manipulate the contents of directories as objects, or move the current directory location between directories. Used in a rake file, it can use Ruby code to deliver commands to the command line as if they were being typed in manually, but as a batch file. + 4. What is an IO object? + 4a. An IO object is an instance of the IO class, a two way channel between Ruby code and a file, the console or some other external read/write resource, including some other resource on the Internet. + 5. What is rake and what is it used for? What is a rake task? + 5a. Rake is a domain specific language Ruby(like Rspec) gem that sends commands to the console (command line/terminal), either by typing them in the console or by running a file (named Rakefile as a default if no file name is specified) to send file creation/alteration tasks to the console. In general, it's used to build file structures needed for some program. + + A rake task is defined by a method name preceded by a colon, invoked by using that method name after the rake command at the console line or some other place that has access to the file, rake tasks can also call other rake tasks. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..9cdc691 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,47 @@ +#@module Worker +class Worker + +=begin trivial case + def work no_of_times=1 + yield + yield + yield + end + +=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 + +=begin named block, trivial case + def self.work no_of_times=1 + no_of_times.times { return yield } + end +=end + +=begin named block, trivial case + def self.work no_of_times=1 + 1.upto(no_of_times) { yield } + end +=end + +end + + + + +# While this fulfills the literal requirement of the test conditions AS STATED including +# "executes a block 3 times and returns the result" (regardless of the parameter passed), +# it does seem to be a trivial non-general case. It should execute the block n times for any +# value of n passed. + +# I have not figured out how to execute yield multiple times, and the things that seem to make +# sense all deliver different results, in part because they seem to redefine the block_given +# in the process of writing a new block. + +# Tried ¶meter naming as an argument, and n parameter.call s, but that didn't work either. \ 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..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