From c90711bbc9caf8db826c5a0b6639e7d7caa53b60 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 9 Oct 2014 09:33:28 -0700 Subject: [PATCH 01/10] homework questions for week 1 --- week1/homework/questions.txt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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. From 2c1da72bab6b49e28fecaed7c35cc34b6962f65b Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 9 Oct 2014 10:34:43 -0700 Subject: [PATCH 02/10] Rspec test. I understand the second two but not the first. will ask in class --- week1/homework/strings_and_rspec_spec.rb | 40 ++++++++++++++---------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..4fc2dc6 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -11,24 +11,30 @@ # (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 + 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 + end - it "should be able to count the charaters" + 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 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 + it "should be able to give the encoding of the string" do + #pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + # 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("US-ASCII") + end + end end From f3e9876426e952fc64046475c2ca1b99950b8e98 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 9 Oct 2014 18:39:47 -0700 Subject: [PATCH 03/10] minor changes in homework --- week1/homework/strings_and_rspec_spec.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 4fc2dc6..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 @@ -18,6 +20,7 @@ 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 @@ -28,12 +31,12 @@ end it "should be able to give the encoding of the string" do - #pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + # 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("US-ASCII") + encoding.should eq Encoding.find("UTF-8") end end end From 5419ed515222bde55c7d7cb14a9baea0bd4f86b5 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Tue, 14 Oct 2014 14:31:10 -0700 Subject: [PATCH 04/10] week 2 questions - hashes, arrays, modules, --- week2/homework/questions.txt | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) 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. From 1579477c7ee126f3f90653a26beb8de6118293bd Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 16 Oct 2014 10:57:33 -0700 Subject: [PATCH 05/10] week 2 homework --- week2/homework/simon_says.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..1bfade0 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,27 @@ +module SimonSays + def echo(word) + word + end + + def shout(word) + word.upcase + end + + def repeat(word) + space = "#{word} " *2 + space.chop + end + + def repeat_times(word, number) + 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 From 7b7211d7e128f1620a561b1b7d0c1c7d9198ee50 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 16 Oct 2014 11:03:08 -0700 Subject: [PATCH 06/10] week 2 --- week2/homework/simon_says.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 1bfade0..b478e5e 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,22 +1,17 @@ module SimonSays def echo(word) - word + word end def shout(word) word.upcase end - def repeat(word) - space = "#{word} " *2 + def repeat(word, number=2) + space = "#{word} " * number space.chop end - def repeat_times(word, number) - space = "#{word} " * number - space.chop - end - def start_of_word(word, number) word.slice(0,number) end From 3a6da8adb2ac1487f7cdff2bbd04fe15e6462913 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 23 Oct 2014 12:55:41 -0700 Subject: [PATCH 07/10] week 3 hw --- week3/homework/questions.txt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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) From 1d8f9b1467b26a2cfafab02e275c44e6123f2d6f Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Thu, 23 Oct 2014 12:56:32 -0700 Subject: [PATCH 08/10] week 3 hw w 1 failing test --- week3/homework/calculator.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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..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 + From 4b8a257b5f5e08cb74b002a4b450f978cf46f9c8 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Tue, 28 Oct 2014 13:12:01 -0700 Subject: [PATCH 09/10] week 4 hw questions --- week4/homework/questions.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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. From b3017727bc150e20a30dbaf8bf7b86523bc5ce59 Mon Sep 17 00:00:00 2001 From: Alicia Goodwin Date: Tue, 28 Oct 2014 20:56:17 -0700 Subject: [PATCH 10/10] week 4 hw method --- week4/homework/worker.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 week4/homework/worker.rb 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