From 56c60330d4fd775bdcc8888a6fdfaed1671574f8 Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Fri, 17 Oct 2014 22:40:32 -0700 Subject: [PATCH 1/4] almost all tests passing --- week3/homework/calculator.rb | 20 ++++++++++++++++++++ 1 file changed, 20 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..a1d6389 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,20 @@ +class Calculator + def sum(values) + values.inject(0) { |result, next_val| result + next_val } + end + + def multiply(*values) + + values.inject { |result, next_val| result * next_val } + end + + def pow(base, exponent) + result = 1 + exponent.times{ result *= base } + result + end + + def fac(value) + value == 0 ? 1 : (1..value).inject { |result, next_val| result * next_val } + end +end \ No newline at end of file From 8286b6c432b06e3e517b19634ba0b96b45b54fcd Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Mon, 20 Oct 2014 22:30:46 -0700 Subject: [PATCH 2/4] finished questions, changed spacing for calculator --- week3/homework/calculator.rb | 10 +++++++--- week3/homework/questions.txt | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index a1d6389..c130e03 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -2,10 +2,14 @@ class Calculator def sum(values) values.inject(0) { |result, next_val| result + next_val } end - def multiply(*values) - - values.inject { |result, next_val| result * next_val } + values_list = Array.new + if (values.first.kind_of?(Array)) + values_list += values.first + else + values_list += values + end + values_list.inject { |result, next_val| result * next_val } end def pow(base, exponent) diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..671955d 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,24 @@ Please Read: 1. What is a symbol? +A: A symbol is Ruby's immutable string-like unique primative, used when creating a variable that stands for something significant, much like enumerables in other languages. An example might be creating a symbol for each direction like :up, :down, :left. :right. + 2. What is the difference between a symbol and a string? +A: Strings are mutable, symbols are immutable. + 3. What is a block and how do I call a block? +A: Blocks are chunks of code inbetween delimeters, curly braces or keywords 'do' and 'end'. Blocks are called with a method name, code wrapped in the do and end keywords and any parameters passed to the block with pipes '|' on either side. + +method_name {|parameter| parameter.work} + 4. How do I pass a block to a method? What is the method signature? +A: You can pass a block to a method by immediately including a block following a method name. The method signature does not have to be special, and does not have to take any special parameters. However to emit values to a block, the method definition needs to contain the yield keyword. + +method_name {|parameter| parameter.work} + 5. Where would you use regular expressions? + +A: Regular expressions are used in any situation that calls for string finding/matching. Regex's can be used for user input validation, manipulation/searching of large text files, or even extraction of values from unstructured data. From 31de8e4c5ffc982e9846628ae8a219eb24061981 Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Mon, 20 Oct 2014 22:37:54 -0700 Subject: [PATCH 3/4] converting from tabs to spaces --- week3/homework/calculator.rb | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index c130e03..037b24b 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,24 +1,24 @@ class Calculator - def sum(values) - values.inject(0) { |result, next_val| result + next_val } - end - def multiply(*values) + def sum(values) + values.inject(0) { |result, next_val| result + next_val } + end + def multiply(*values) values_list = Array.new - if (values.first.kind_of?(Array)) - values_list += values.first - else - values_list += values - end - values_list.inject { |result, next_val| result * next_val } - end + if (values.first.kind_of?(Array)) + values_list += values.first + else + values_list += values + end + values_list.inject { |result, next_val| result * next_val } + end - def pow(base, exponent) - result = 1 - exponent.times{ result *= base } - result - end + def pow(base, exponent) + result = 1 + exponent.times{ result *= base } + result + end - def fac(value) - value == 0 ? 1 : (1..value).inject { |result, next_val| result * next_val } - end + def fac(value) + value == 0 ? 1 : (1..value).inject { |result, next_val| result * next_val } + end end \ No newline at end of file From e5f79926204218b58fc788f6d912dca3f756fd4b Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Wed, 29 Oct 2014 22:14:01 -0700 Subject: [PATCH 4/4] all tests passing, questions completed --- week4/homework/questions.txt | 18 ++++++++++++++++++ week4/homework/worker.rb | 7 +++++++ 2 files changed, 25 insertions(+) create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..28d9d80 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,10 +4,28 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +A: Call File.open on the file in the file system, and then use a block to call something on the file object iterator, typically something like the each_line method. Each line will yield a string value for line of the file. + +File.open("aFile.txt") do |file| + file.each_line {|line| puts line } +end + 2. How would you output "Hello World!" to a file called my_output.txt? +A: Call File open on my_output.txt, passing a "w" for as the mode param (write), and then call puts "Hello World!" on the resulting file object. + +File.open("my_output.txt" "w") do |file| + file.puts "Hello World!" +end + 3. What is the Directory class and what is it used for? +A: The directory class is a standard ruby library class that represent directories (folders) within a host machine's local file system. It provides multiple methods to enumerate directories and files within those directories. + 4. What is an IO object? +A: An IO object is a bidirectional channel between a Ruby program and external resources like files or sockets. Files and sockets are subclasses of the IO object and support similar interfaces. + 5. What is rake and what is it used for? What is a rake task? + +A: Rake is a build utility that is used to provide an easy to use domain specific language to define/automate common tasks that a developer may commonly run within a ruby project. A rake task is similar to a function, you can call a particular rake task by name and it will run whatever operations defined for that task. A task example might be setting some environment variable used by a ruby program. \ No newline at end of file diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..ba659dd --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,7 @@ +module Worker + def self.work(n = 1) + val = nil + n.times {val = yield} + return val + end +end \ No newline at end of file