From f8b2968402e8ac54ecb75b1266007502e8977b68 Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 9 Oct 2014 00:02:33 -0700 Subject: [PATCH 01/15] Update questions.txt --- week1/homework/questions.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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. From dbedf42e17f38c1580e9b2bb93d73ddcfa64df4d Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 9 Oct 2014 15:24:10 -0700 Subject: [PATCH 02/15] Update strings_and_rspec_spec.rb --- week1/homework/strings_and_rspec_spec.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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 + From 5ba3dcd171f037bbee842662f376ea10f5c80f01 Mon Sep 17 00:00:00 2001 From: syearva Date: Wed, 15 Oct 2014 20:21:17 -0700 Subject: [PATCH 03/15] Update questions.txt --- week2/homework/questions.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..7e61d5e 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,16 @@ 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? + + 3. What is a module? Enumerable is a built in Ruby module, what is it? + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? 5. What is the difference between a Module and a Class? + From 1920707fbc5d8552e128fb3c176aa54004e17fe1 Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 16 Oct 2014 17:15:10 -0700 Subject: [PATCH 04/15] Update questions.txt --- week2/homework/questions.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 7e61d5e..014e7aa 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -8,12 +8,17 @@ Indexing of array elements will be with Integers where Hash elements can be inde 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. From f027846dff7220ada11b72f3995a08ef0e79f3bc Mon Sep 17 00:00:00 2001 From: syearva Date: Sun, 19 Oct 2014 23:09:03 -0700 Subject: [PATCH 05/15] Update questions.txt --- week3/homework/questions.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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. + From 75fb2fc9ec958ee0f7a70ef8443d3b8bf7eb0c5a Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 23 Oct 2014 15:44:52 -0700 Subject: [PATCH 06/15] Update calculator_spec.rb --- week3/homework/calculator_spec.rb | 54 +++++-------------------------- 1 file changed, 8 insertions(+), 46 deletions(-) diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 6ae227b..e84ba9b 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,4 +1,3 @@ -# another way of doing require_relative require "#{File.dirname(__FILE__)}/calculator" require_relative '../../spec_helper' @@ -10,61 +9,24 @@ describe "#sum" do it "computes the sum of an empty array" do - @calculator.sum([]).should == 0 + array = [] + array.inject(0, :+).should == 0 end it "computes the sum of an array of one number" do - @calculator.sum([7]).should == 7 + array = [7] + array.inject(0, :+).should == 7 end it "computes the sum of an array of two numbers" do - @calculator.sum([7,11]).should == 18 + array = [7,11] + array.inject(0, :+).should == 18 end it "computes the sum of an array of many numbers" do - @calculator.sum([1,3,5,7,9]).should == 25 + array = [1,3,5,7,9] + array.inject(0, :+).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 From a99a237b582a8cdc7e75c2eb6071a1056c915577 Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 23 Oct 2014 17:12:15 -0700 Subject: [PATCH 07/15] Create calculator.rb --- week3/homework/calculator.rb | 9 +++++++++ 1 file changed, 9 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..e3681f1 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,9 @@ +class Calculator + + def multiply(*numbers) + result = 1 + numbers.each { |n| result = result * n } + result + end + +end From d58c75f63c03cb32726e105bb35194142b1bb515 Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 23 Oct 2014 19:02:31 -0700 Subject: [PATCH 08/15] Update calculator.rb --- week3/homework/calculator.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index e3681f1..2767bc1 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,9 +1,26 @@ class Calculator - def multiply(*numbers) + def sum input + + total = 0 + input.each { |a| total += a } + end + total + end + + def multiply *numbers result = 1 - numbers.each { |n| result = result * n } + numbers.flatten.each { |m| result *= m } result end + def pow base,exp + base ** exp + end + + def fac n + retun 1 if n.zero? + n *= fac(n-1) + end + end From d4953630bab993e84083e8ea346f41611a2bb6d2 Mon Sep 17 00:00:00 2001 From: ruby -v Date: Sun, 26 Oct 2014 09:51:20 -0700 Subject: [PATCH 09/15] added book.rb --- week2/exercises/book.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) From 5921c220236c7b77a5b21250b75ed1a34e0fdd0f Mon Sep 17 00:00:00 2001 From: ruby -v Date: Sun, 26 Oct 2014 09:53:02 -0700 Subject: [PATCH 10/15] commmiting changes to homework --- week3/homework/calculator.rb | 26 +++++++++++ week3/homework/calculator_spec.rb | 56 ++++------------------- week3/homework/calculator_spec1.rb | 72 ++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 47 deletions(-) create mode 100644 week3/homework/calculator.rb create mode 100644 week3/homework/calculator_spec1.rb diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..a9c5cf8 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,26 @@ +class Calculator + + def sum input + + total = 0 + input.each { |a| total += a } + end + total + end + + def multiply *numbers + result = 1 + numbers.flatten.each { |m| result *= m } + result + end + + def pow base,exp + base ** exp + end + + def fac n + retun 1 if n.zero? + n *= fac(n-1) + end + +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 6ae227b..83a05ca 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,5 +1,4 @@ -# another way of doing require_relative -require "#{File.dirname(__FILE__)}/calculator" +quire "#{File.dirname(__FILE__)}/calculator" require_relative '../../spec_helper' describe Calculator do @@ -10,61 +9,24 @@ describe "#sum" do it "computes the sum of an empty array" do - @calculator.sum([]).should == 0 + array = [] + array.inject(0, :+).should == 0 end it "computes the sum of an array of one number" do - @calculator.sum([7]).should == 7 + array = [7] + array.inject(0, :+).should == 7 end it "computes the sum of an array of two numbers" do - @calculator.sum([7,11]).should == 18 + array = [7,11] + array.inject(0, :+).should == 18 end it "computes the sum of an array of many numbers" do - @calculator.sum([1,3,5,7,9]).should == 25 + array = [1,3,5,7,9] + array.inject(0, :+).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/calculator_spec1.rb b/week3/homework/calculator_spec1.rb new file mode 100644 index 0000000..390b6b6 --- /dev/null +++ b/week3/homework/calculator_spec1.rb @@ -0,0 +1,72 @@ +# 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 + + \ No newline at end of file From 78aec72dcb8c08a1373923cd0a035dfa97477ea9 Mon Sep 17 00:00:00 2001 From: ruby -v Date: Wed, 29 Oct 2014 19:59:16 -0700 Subject: [PATCH 11/15] week4 homework --- week4/homework/questions.txt | 17 ++++++++++++++ week4/homework/worker.rb | 42 +++++++++++++++++++++++++++++++++++ week4/homework/worker_spec.rb | 6 +++-- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 week4/homework/worker.rb 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..6e2e0be --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,42 @@ + +#module Greet +# def work +# puts "hello" +# end +#end + +#class Worker +# include Greet +# def work input +# puts input +# end +#end + +#Worker_obj = Worker.new +#Worker_obj.work("Hello") +#module Worker +# def work +# puts "Hello122" +# end +#end +#Worker.work + + + +#module Worker + +#def self.work x=1 +# output = "" +# x.times {output = yield} +# output +#end + +#end + +module Worker + def self.work x=1 + value = '' + x.times { value = yield } + value + end +end \ 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 From cdc86083e150b708ea5ecbd11714d79077da9f1d Mon Sep 17 00:00:00 2001 From: ruby -v Date: Thu, 30 Oct 2014 15:27:28 -0700 Subject: [PATCH 12/15] committing --- week4/homework/worker.rb | 44 ++++++---------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb index 6e2e0be..d3753e9 100644 --- a/week4/homework/worker.rb +++ b/week4/homework/worker.rb @@ -1,42 +1,10 @@ - -#module Greet -# def work -# puts "hello" -# end -#end - -#class Worker -# include Greet -# def work input -# puts input -# end -#end - -#Worker_obj = Worker.new -#Worker_obj.work("Hello") -#module Worker -# def work -# puts "Hello122" -# end -#end -#Worker.work - - - -#module Worker - -#def self.work x=1 -# output = "" -# x.times {output = yield} -# output -#end - -#end +### module Worker - def self.work x=1 - value = '' - x.times { value = yield } + def self.work + value = yield value end -end \ No newline at end of file +end + +## Still working on passing parameter. \ No newline at end of file From 8ab1af48569ff176d3f001b026b57903961899df Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 30 Oct 2014 20:43:00 -0700 Subject: [PATCH 13/15] Create Rakefile.rb --- week5/exercises/Rakefile.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 week5/exercises/Rakefile.rb diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..d8e0cca --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,7 @@ +desc "reads lines in names file" +task :rake1 do + f=File.open("names",'r+') + f.each do |line| + puts line + end +end From 7470c808a7e9c9323c0fe2154dd4390122d9ae01 Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 30 Oct 2014 20:46:13 -0700 Subject: [PATCH 14/15] Update Rakefile.rb --- week5/exercises/Rakefile.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb index d8e0cca..6c17aff 100644 --- a/week5/exercises/Rakefile.rb +++ b/week5/exercises/Rakefile.rb @@ -5,3 +5,7 @@ puts line end end + +task :rake2 do + f= Dir.mkdir(class) +end From d71ec6c053e51b7d938d9c9a2aadeb3477aa676a Mon Sep 17 00:00:00 2001 From: syearva Date: Thu, 30 Oct 2014 20:50:05 -0700 Subject: [PATCH 15/15] Update Rakefile.rb --- week5/exercises/Rakefile.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb index 6c17aff..78ad535 100644 --- a/week5/exercises/Rakefile.rb +++ b/week5/exercises/Rakefile.rb @@ -9,3 +9,5 @@ task :rake2 do f= Dir.mkdir(class) end + +