diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..9e9473a 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,34 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +Object is an Instance of class or variable of class. + 2. What is a variable? +Variables are the memory locations which hold any data to be used by any program. +There are different types of variables supported by Ruby. + +Global +Local +Class +Pseudo + 3. What is the difference between an object and a class? +a class is a construct that defines a collection of properties and methods. Memory space is not allocated , when it is created. +an Object is an Instance of class or variable of class. Memory space is allocated, when it is created. + 4. What is a String? +A String object in Ruby holds and manipulates an arbitrary sequence of one or more bytes, typically representing characters that represent human language. + 5. What are three messages that I can send to a string object? Hint: think methods +substitution (gsub,replace), insertion ( insert) , Changing a section ([]) + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +SIngle quoted strings +Double quotes strings + +Main difference is Double quotes support interpolation where as Single Quotes doesn't. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..85c51e8 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -16,19 +16,26 @@ @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 + result = @my_string.length + result.should be 66 + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + + result = @my_string.split('.') + #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 + #pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + result = @my_string.encoding + result.should be Encoding.find("UTF-8") + #do something with @my_string here #use helpful hint here end end end + diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 34931fa..df7ca06 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -14,4 +14,6 @@ def initialize title = "Not Set", page_count = 0 @title = title end -end \ No newline at end of file +end + +aBook = Book.new( "Test", 10) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..014e7aa 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,21 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +Indexing of array elements will be with Integers where Hash elements can be indexed with any type of elements, doesn't need to be integer . This makes hash indexing will not be in order, but by key value. + 2. When would you use an Array over a Hash and vice versa? +Array might be preferred over Hash when order of the elements is known ( especially when elements are less in number). +Hash is preferred over Array when elements can be accesssed by key value. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +Module is a collection of methods and constants. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +mixins in Ruby will be helpful in sharing code. + 5. What is the difference between a Module and a Class? + +Modules are about providing methods that you can use across multiple classes. We can think about them as "libraries" . Classes are about objects; modules are about functions. diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb deleted file mode 100644 index 6ae227b..0000000 --- a/week3/homework/calculator_spec.rb +++ /dev/null @@ -1,70 +0,0 @@ -# another way of doing require_relative -require "#{File.dirname(__FILE__)}/calculator" -require_relative '../../spec_helper' - -describe Calculator do - - before do - @calculator = Calculator.new - end - - describe "#sum" do - it "computes the sum of an empty array" do - @calculator.sum([]).should == 0 - end - - it "computes the sum of an array of one number" do - @calculator.sum([7]).should == 7 - end - - it "computes the sum of an array of two numbers" do - @calculator.sum([7,11]).should == 18 - end - - it "computes the sum of an array of many numbers" do - @calculator.sum([1,3,5,7,9]).should == 25 - end - end - - # Once the above tests pass, - # write tests and code for the following: - describe "#multiply" do - it "multiplies two numbers" do - @calculator.multiply(2,2).should eq 4 - end - - it "multiplies an array of numbers" do - @calculator.multiply([2,2]).should eq 4 - end - end - - it "raises one number to the power of another number" do - p = 1 - 32.times{ p *= 2 } - @calculator.pow(2,32).should eq p - end - - # http://en.wikipedia.org/wiki/Factorial - describe "#factorial" do - it "computes the factorial of 0" do - @calculator.fac(0).should eq 1 - end - it "computes the factorial of 1" do - @calculator.fac(1).should eq 1 - end - - it "computes the factorial of 2" do - @calculator.fac(2).should eq 2 - end - - it "computes the factorial of 5" do - @calculator.fac(5).should eq 120 - end - - it "computes the factorial of 10" do - @calculator.fac(10).should eq 3628800 - end - - end - -end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..e735d57 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,25 @@ Please Read: 1. What is a symbol? +A Symbol is the most basic Ruby object you can create. It's just a name and an internal ID. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program. + 2. What is the difference between a symbol and a string? +Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory. + 3. What is a block and how do I call a block? +A block consists of chunks of code. You assign a name to a block.The code in the block is always enclosed within braces ({}). +A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block. +You invoke a block by using the yield statement. + + 4. How do I pass a block to a method? What is the method signature? + if the last argument of a method is preceded by &, then you can pass a block to this method and this block will be assigned to the last parameter. + In case both * and & are present in the argument list, & should come later. + 5. Where would you use regular expressions? + +while matching patterns in strings. + diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..7e3852b 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,10 +4,27 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +## File.new("testfile", "r") +## File.open("testfile", "r") + 2. How would you output "Hello World!" to a file called my_output.txt? +## File.open(my_output.txt, 'w') { |file| file.write("Hello World!") } +## File.write('my_output.txt', 'Hello World!') + 3. What is the Directory class and what is it used for? +## Objects of class Dir are directory streams representing directories in the +## underlying file system. They provide a variety of ways to list directories +## and their contents. + + 4. What is an IO object? +## The IO class is the basis for all input and output in Ruby. IO is superclass of File. + 5. What is rake and what is it used for? What is a rake task? + +## Rake is make like program in Ruby. +## A Task is the basic unit of work in a Rakefile. Tasks have associated actions (possibly more than one) and a ## list of prerequisites. When invoked, a task will first ensure that all of its prerequisites have an +## opportunity to run and then it will execute its own actions. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..d3753e9 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,10 @@ +### + +module Worker + def self.work + value = yield + value + end +end + +## Still working on passing parameter. \ No newline at end of file diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb index 993cd49..d0d2879 100644 --- a/week4/homework/worker_spec.rb +++ b/week4/homework/worker_spec.rb @@ -2,12 +2,14 @@ require "#{File.dirname(__FILE__)}/worker" describe Worker do - - it "executes a block and returns a string" do + + it "executes a block and returns a string" do + result = Worker.work do "hello" end result.should == "hello" + end it "executes a block and returns a number" do diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..78ad535 --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,13 @@ +desc "reads lines in names file" +task :rake1 do + f=File.open("names",'r+') + f.each do |line| + puts line + end +end + +task :rake2 do + f= Dir.mkdir(class) +end + +