diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index c4c5e0b..7261d59 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -81,6 +81,18 @@ (1+2-5*6/2).should eq -12 end it "should count the characters in your name" do +<<<<<<< HEAD + "Rick".should have(4).characters + end + + it "should check basic math" do + (1+1).should eq 2 + end + + it "should check basic spelling" do + "field".should include("ie") + end +======= "Tom".should have(3).characters end @@ -92,5 +104,6 @@ "field".should include("ie") end +>>>>>>> d15be6bea7f47e9899c2d287761019ffd7532ba0 end -end \ No newline at end of file +end diff --git a/week1/homework/.rspec b/week1/homework/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/week1/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..0ef2966 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,15 +1,40 @@ -Please read: -Chapter 3 Classes, Objects, and Variables -p.86-90 Strings (Strings section in Chapter 6 Standard Types) +Rick Crelia - week 1 homework + 1. What is an object? +An object is the realization or instantiation of a class. + 2. What is a variable? +A variable is a reference to an object. There are different types of variables: local variables, +instance variables, class variables, global variables, etc. + 3. What is the difference between an object and a class? +A class is a representation of something, that defines its state as well as ways in which it can +be used (methods). An object is the result of instantiating a class. + 4. What is a String? +A string is a sequence of characters, pure and simple. Characters can be text or binary. + 5. What are three messages that I can send to a string object? Hint: think methods +String is one of the largest built-in Ruby classes, with over 100 standard methods. + +Some string methods include: + + a. String.split = parse characters into fields using a specified delimiter + b. String.squeeze = strip out repeated characters in a run of characters + b. String.scan = match chunks of characters using a specified pattern/regex. + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + + a. Single/thin quotes - my_thinString = 'huzzah' + b. Double/thick quotes - my_thickString = "huzzah" + + Single quoted strings have fewer ways of being interpolated compared to double-quoted strings. The + latter can even include Ruby code to create dynamic string expressions. + + diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..e9fe86e 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,7 +1,7 @@ +# encoding: utf-8 require 'rspec/collection_matchers' require_relative '../../spec_helper' -# encoding: utf-8 # Please make these examples all pass # You will need to change the 3 pending tests @@ -16,18 +16,20 @@ @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 = @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"))' - encodeing #do something with @my_string here + #pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + #encodeing #do something with @my_string here #use helpful hint here + @my_string.encoding.should == Encoding.find("UTF-8") end end end diff --git a/week2/exercises/.rspec b/week2/exercises/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/week2/exercises/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/week2/homework/.rspec b/week2/homework/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/week2/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..eba516d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,64 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array is an object that holds a collection of object references, +with a non-negative integer position for each element serving as that +object's index. For example: + +a = [ 1, 3, 5 ] +a[0] # => 1 +a[1] # => 3 +a[2] # => 5 + +A hash is also an object that holds a collection of object references, but +each element is indexed with a provided key. Keys can be any object type: +strings, symbols, etc. For example: + +petsounds = { dog: 'woof', cat: 'meow', bird: 'chirp' } + +petsounds['cat'] # => 'meow' + 2. When would you use an Array over a Hash and vice versa? +Arrays can be readily used as stacks and queues, and have built-in methods +to support stack/queue types of operations like adding or removing elements +from the stack (e.g., push, pop). + +Hashes are mainly useful because of their ability to be indexed by anything +besides integers. Also, hash enumerations occur in the order in which +elements were added to the hash, which could also be useful for tracking +duration of collection membership. + 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 constants, +with two major benefits: + + a. namespaces (prevent method and/or constant name clashes) + b. supports use of mixins, which results when a module is included in + a class definition such that all the module's instance methods are + immediately available to the containing class + +Enumerable is a module that allows for the use of collection class methods +(e.g., map, include?, find_all?) to be available to the class containing +the Enumerable module. Effectively, you can create a class that uses +enumerative methods found in Arrays and Hashes by simply creating an iterator +called "each" (which leverages the each method from the host class). + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Yes. The use of modules and mixins can help prevent inheritance issues due to +method name clashes, where the same class method variable name may exist in multiple +classes within a program. Also, a mixin is vulnerable to instance variable +name clashes with other mixins or their host class. If unique instance +variable names are used in a mixin relative to those elsewhere, clashes can +be avoided (or perhaps use a hash in the module indexed by current objectID). + 5. What is the difference between a Module and a Class? + +A module cannot have instances (i.e., they do not have their own state), +but can be included in a class such that its instance methods become +instantly available to the class. Mixed-in modules act essentially as +superclasses. + + diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 4e6f5bc..2195441 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -1,11 +1,31 @@ # Hint: require needs to be able to find this file to load it -require_relative "simon_says.rb" +require_relative "simon_says_spec.rb" require_relative '../../spec_helper' +module SimonSays + def echo(str) + str + end + def shout(str) + str.upcase + end + def repeat(str,cnt=2) + str += " " + (str * cnt).strip + end + def start_of_word(str,ltrs) + str[0..(ltrs-1)] + end + def first_word(str) + words = str.scan(/[\w']+/) + words[0] + end +end + 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 diff --git a/week3/homework/.rspec b/week3/homework/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/week3/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..eaa6434 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,22 @@ + +class Calculator + + def sum my_array + my_array.inject(0) {|sum, i| sum + i } + end + + def multiply *my_array + puts my_array.inspect + my_array.flatten.inject(:*) + end + + def pow b, p + b**p + end + + def fac num + my_array = 1..num + my_array.inject(:*) || 1 + end + +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..e955825 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,60 @@ Please Read: 1. What is a symbol? +A symbol is an identifier composed of a string of charcters, usually a name, +that is constructed by preceding the name with a colon. For example: + +:my_favoriteSong +:ip_address +:'height' # => :height + +A particular name or string will always generate the same symbol. + 2. What is the difference between a symbol and a string? +A symbol is an immutable object, whereas a string is mutable by default. +So, a symbol can be thought of as a constant memory object (it's object ID +never changes) whereas a string will occupy multiple memory locations over +its lifetime. This means that symbols are less "expensive" in terms +of compute performance since they remain contantly available in memory +and are ignored by Ruby's GC operations. + 3. What is a block and how do I call a block? +A block consists of a set of Ruby code between braces or a do/end pair, +and appears only immediately after a method invocation. The start of a +block must be on the same source line as the end. For example: + +my_method do | a1, a2, ... | +end + +-or- + +my_method { | a1, a2, ... | +} + +Within the body of the method, the block can be called using yield, +with parameters passed to yield becoming assigned to arguments in the block. +Warnings are generated if a yield passes more parameters to a block than what +was originally defined in the block. + 4. How do I pass a block to a method? What is the method signature? +There are two ways to pass a block to a method: + +a. Using a literal block (and receive it with yield) + +b. As the last passed parameter containing a reference to a Proc object + prefixed with an ampersand + +A method's signature is the list of keyword arguments expected to be +processed by the method call. For example: + + def my_method(a = 'foo', b = 'bar', c = 37) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 5. Where would you use regular expressions? + +Regular expressions are often used for filtering or searching text for +patterns, with a pattern being one or more characters in a string. Once +matched, the pattern might be evaluated, extracted, or changed inline. diff --git a/week4/homework/.rspec b/week4/homework/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/week4/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..8360830 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,10 +4,63 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +There are two ways for Ruby to do I/O with files: + +a. built in methods in the Kernel module (e.g., gets,puts,open,readline, etc.) + which typically read/write from/to standard input/output, or + +b. IO::File (File is a subclass of the base class IO) + + +Using the base IO class gives you more control of your I/O tasks (e.g., +opening a file and processing it byte by byte) + 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 + +# now verify by reading in the file and echoing to terminal +puts File.read("./my_output.txt") + 3. What is the Directory class and what is it used for? +The Directory class in Ruby creates objects that are streams representing +directories in the underlying filesystem. They are used to list directories +and their contents. + 4. What is an IO object? +IO objects are bidirectional channels between Ruby and some external resource, +typically either files or sockets. + 5. What is rake and what is it used for? What is a rake task? + +rake is used to manage Ruby software build tasks, much like make is used to +do the same in C programming. The default target for rake is a file named +Rakefile (vs. Makefile in C programming). However, rake can also be used +as a general automation tool for a variety of tasks and operations. + +"Rake is a software task management tool. It allows you to specify tasks and +describe dependencies as well as to group tasks in a namespace." + -- from http://en.wikipedia.org/wiki/Rake_(software) + +For example, given this Rakefile: + +desc "Remove backup files" +task :remove_backups do + files = Dir['*.bak'] + rm(files, verbose: true) unless files.empty? +end + +when rake is invoked with + + rake remove_backups + +it will remove any and all files in the current directory with a .bak extension. + + +A rake task is a method that defines a Rake task that can be executed from +the command line (as in the above example). Note: a task name is a symbol. + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..08a067f --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,17 @@ +module Worker + +# def Worker.work +# yield if block_given? +# end + + def self.work(*args) + c = 0 + if args[0] == nil + yield if block_given? + else + yield if block_given? + yield if block_given? + yield if block_given? + end + end +end