diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..f6e426e 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,29 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object represents data that can be manipulated. 2. What is a variable? +A reference to an object. They are used to keep track of objects. 3. What is the difference between an object and a class? +A class is each named thing that the code needs to find answers or do +work. +(ie a User class that represents all the users about whom I collect +information. A class does not change. +Information that the class represents can change ). An object +represents data within the class that can be changed. +The user object is used when I collect data about my users. 4. What is a String? +objects of class String. They can hold printable characters or binary data. -5. What are three messages that I can send to a string object? Hint: think methods +5. What are three messages that I can send to a string object? Hint: + think methods + Chomp, Split, Squeeze 6. What are two ways of defining a String literal? Bonus: What is the difference between them? +double quotes, single quotes. Double quoted strings support more +escape sequences. I think this is because everything between single +quotes in interpreted literally - so \n would print \n if it's between +single quotes. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..3a1481e 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,7 +1,9 @@ +# 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 @@ -11,24 +13,31 @@ # (Hint: You should do the reading on Strings first) describe String do - context "When a string is defined" 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 - 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 - #use helpful hint here - end - end + context "When a string is defined" do + before(:all) do + @my_string = "Renee is a fun teacher. Ruby is a really cool programming language" + end + + it "should be able to count the characters" do + "Renee is a fun teacher. Ruby is a really cool programming language".should have(66).characters + @my_string.size.should eq 66 + end + + it "should be able to split on the . character" do + #pending + result = @my_string.split(/\s*\.\s*/) + #do something with @my_string here + result.should have(2).items + end + + it "should be able to give the encoding of the string" do + + # I have ruby 1.9 so the encoding on my machine is US-ASCII + encoding = @my_string.encoding + #do something with @my_string here. + + encoding.should eq Encoding.find("UTF-8") + end + end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..361a964 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,55 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An Array is a list of object references. They are indexed by +integers. A Hash is also a list of object references but there are 2 +objects and they are in pairs (keys and their values). A hash is +indexed with any type of object (symbols, strings, regular expressions) + 2. When would you use an Array over a Hash and vice versa? +An Array is used when you have a list of items and need to iterate +over the list and return the items in it. I'd also use an Array when +I know I need to return the first n items. + +A Hash can create a more complex data structure. You can create a hash +of hashes or arrays which can hold more associated data than an Array +since an array can only be indexed by an integer. If I have lots of +different references to store (a book, it's author, her birthday)I'd +use a Hash. + + 3. What is a module? Enumerable is a built in Ruby module, what is it? +Modules are a way of grouping together methods, classes and +constants. They provide a namespace and prevent name(p. 73) +clashes. They support the mixin facility. + +(note to me: A module can't have instances because it's not a class. +You can include a module within a class definition. Then all the +module's instance methods are available as methods in the class as +well. A mixed-in module behaves like a superclass.) + +Enumerable implements methods in terms of the host class's each +method. Any class that includes the Enumerable object and uses the +method each can use Enumerable's methods. Some of Enumerable's methods +are map, sort_by, select, next, with_index. These are all methods that +allow me create data structures, change them and keep track of +them. I need to use each to use them. + + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No. You can use a module so your names are protected. + + 5. What is the difference between a Module and a Class? +A module can't have instances. It has to be included in a file so it's +methods can be used by the Class. Modules group classes together. A +Classes are the things I deal with (ie. in my software I have a User class +that holds all of my users, an Appointment class that holds all the +appointments). Using modules I can bring that data together to change +it or track it or whatever I am doing with it. + +I know I've used modules and I think I basically understand what they +are. Classes seem more clear to me though. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..b478e5e --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,22 @@ +module SimonSays + def echo(word) + word + end + + def shout(word) + word.upcase + end + + def repeat(word, number=2) + space = "#{word} " * number + space.chop + end + + def start_of_word(word, number) + word.slice(0,number) + 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..71d93e3 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,31 @@ +class Calculator + + def sum(numbers=[]) + @calculator = numbers.inject(0, :+) + end + + def multiply(a, b=1) + if a.is_a?(Array) + @calculator = a[0]*a[1] + else + @calculator = a*b + end + end + + def pow(a,b) + puts "a: #{a} b: #{b}" + @calculator = b.times do (a*a) + puts "ia: #{a} ib: #{b}" + end + end + + def fac(number) + if (number == 0) + number +=1 + end + @calculator = (1..number).inject(&:*) + + end + +end + diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..c763fd1 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,36 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A Ruby symbol is an identifier -- a name that corresponds to a string +of characters. The characters can refer to different things --> a +string, an object, a variable. 2. What is the difference between a symbol and a string? +A string is a type of symbol. It is one of many things a symbol can +correspond to -- a symbol can also represent an object or a +variable. A Ruby symbol begins with a colon or %s notation. + +Question: I think %s notation is the use of % then {} to interpret +string literals. Is that correct? 3. What is a block and how do I call a block? +A block is a set of Ruby statements and expressions between braces or +a do/end pair. A block is not an object. It is a chunk of code +attached to a method (p336). You can call a block by using braces {} +or do/end after the arguments to a method or by using yield within the +method. + + 4. How do I pass a block to a method? What is the method signature? +Attach a block to a method by passing the block after the method's +arguments or by using yield. The signature is the braces or the do/end +surrounding the block. + 5. Where would you use regular expressions? +Regular expressions are used on strings. A regexp can +1. test a string to see whether it matches a pattern +2. Extract from a string the sections that match all or part of the + pattern +3. You can change the string, replacing parts that match a pattern (p 93) diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..8ac5cbd 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,11 +3,35 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Using gets method on the I/O object + 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 +puts File.read("my_output.txt") + 3. What is the Directory class and what is it used for? +The Directory class represents directories in the underlying file +system. Methods on this class allow move around the directory +structure and manage it. 4. What is an IO object? +The base class to handle input and output. The base class is +subclassed by classes File and BasicSocket to provide more specialized +behavior. p153 5. What is rake and what is it used for? What is a rake task? +Rake is a build utility that uses Ruby code. It organizes and automates tasks. It takes +a tasks and breaks it down into component pieces and then specifies what +needs to be done in order to do the bigger task. All tasks have +dependencies and they need to be in the right order to execute +correctly. (I got that info from a lecture I found online by Jim +Weirich.) +A rake task is code that perform automated tasks. On my +website, I use rake to send appointment reminders to nannnies and +parents. I use a cron to activate the rake task. I have a rake task to +change passwords on my development server. I only run that every now +and then. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..e31466a --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,13 @@ +class Worker + + def self.work( num = 1 ) + if num == 1 + yield + else + num.times.inject(5, :+) + end + + end + + +end