Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

week 7 homework for Remy #121

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
1a. An object is anything that can be manipulated by another operation,and the results of those manipulations are also objects. They usually begin as instances of Classes, which

2. What is a variable?
2a. A variable is a collection of lettters and/or numbers, starting with a lower case letter (or for an instance variable, a @, or a global variable a $), to which value is assigned by putting the variable on the left followed by a single equal sign followed on the right by the value being placed into the variable. That value can be changed.

3. What is the difference between an object and a class?
3a. A class is the general description of something (a real world thing or a concept with characteristics) and an object is an instance--a specific case--of that general class. Objects can also be created from other objects. An object is something that can be manipulated by code.

4. What is a String?
4a. A string is an ordered collection of usually but not always printable individual characters in whatever encoding is present when they're put together. Simplisticaly and usually, a string is a series of letters and numbers. They can be typed in final form from a keyboard or assembled on the fly with code.

5. What are three messages that I can send to a string object? Hint: think methods
5a. (1) .length returns the number of characters in the string; (2) .capitalize returns a string that copies the string and changes it so the first letter of the first word is capitalized and all other letters are lower case; (3) .capitalize! changes the original string stored in a variable name to capitalize the first letter of the first word and convert all other letters are lower case; if changes are made, it returns the changed string; if no changes are made, it returns nil.

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?

6a. Single quotes and double quotes surrounding what, as a result of the presence of those quotes, is defined as the string literal.

6bonus. Single quotes allow for limited internal interpolation (substitution of processed information), identified so far as only an escape (backslash) before the single quote (so it doesn't end the literal) and an escape (backslash) before another backslash, so you can treat the backslash as a printable text character. Double quotes allow for a broader range of '\'-initiated escape sequences (like a new line feed, \n), as well as code-interpreted renderings of information, such as the values of variables, calculations, expressions and (presumably) anything that can be handled as an object. These interpolations--renderings of code--occur inside the '#{' opener and '}' closer.
23 changes: 23 additions & 0 deletions week2/exercises/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@ class Book
attr_accessor :title
attr_reader :page_count

<<<<<<< HEAD
attr_accessor :title, :page_count

@@book_count = 0 # sets initial value for class

def self.book_count
@@book_count # cless level
end

def initialize title = "Not Set" page_count = 0
@@book_count += 1
@page_count = page_count
@title = title
end

def test
@test = "Hello"
end

def output_test
puts @test
=======
@@book_count = 0

def self.book_count
Expand All @@ -23,5 +45,6 @@ def out_put_test
puts @test
puts @@book_count
end
>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7

end
19 changes: 19 additions & 0 deletions week2/exercises/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

describe Book do

<<<<<<< HEAD
context "::book_count" do
it "should count how many books have been created"
end

before :each do
@book = Book.new
end

it "should respond to title" do
@book.should respond_to "title"
end

it "should allow me to set the title" do
@book.title = "Snow Crash"
@book.title.should eq "Snow Crash"
end
=======

context "::book_count" do

Expand Down Expand Up @@ -45,5 +63,6 @@


end
>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7

end
17 changes: 16 additions & 1 deletion week2/exercises/mad_libs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
puts "Enter a present tense verb"
verb = gets.chomp
puts "Enter a formal noun"
name = gets.chomp
puts "Enter an adverb"
adverb = gets.chomp
puts "Enter an adjective"
adjective = gets.chomp
puts "Enter a common noun"
noun = gets.chomp
story = "#{name} #{verb} the #{adjective} #{noun} #{adverb}"
puts story
puts


puts "Please enter a noun"
noun = gets.chomp
puts "Please enter an adjective"
Expand All @@ -7,4 +22,4 @@
puts "What does the #{noun} say?"
says = gets.chomp
story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}"
puts story
puts story
5 changes: 5 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
1a. An array is an ordered collection of objects, referenceable by its sequential position in the array, or just by looking for it or some characteristic of it. A hash is an unordered collection of objects, referenceable by a key that refers to the value following the key. In a hash, you use the key to 'search' for the value of the key.

2. When would you use an Array over a Hash and vice versa?
2a. An array is a very efficient way to handle collections of objects that exist without reference to any other object. A hash is more useful when objects have another piece of information they relate to or are the values of, and that information is useful for identifying the related value.

3. What is a module? Enumerable is a built in Ruby module, what is it?
3a. A module is a block of code that starts with the word 'module,' followed by the name of the module and ends with a matching 'end.' A module is way of grouping methods, classes and constants and defines a controllable namespace that can be called unambigously by defining referencable objects using the name of the module followed by a period followed by the name of the referenceable object. Using the enter module.object combination, the names can be referenced unambiguously. Enumerable, as a standard mixin, is a module that doesn't need to be explicity included, and it brings a host of operations for dealing with collections of objects, including sorting, finding and testing conditions.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
4a. An instance of a class can inherit the attributes and functionality of its parent class, but it can't inherit from multiple parents. This can be gotten around by using mixins, which reference modules with attributes and functionalities different from the parent class.

5. What is the difference between a Module and a Class?
5a. While both are constructed in Ruby code, and many of both exist in the standard Ruby library, a module is a general collection of code, and can include classes and other namespaced code blocks. A class is a generalize description of a type (class) of object, of which there will be many specific instances. Classes can also have subclasses (children) that inherit the attributes and functionality of the parent class. Modules are not designed to have instances that inherit.
24 changes: 24 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SimonSays

def echo greeting
greeting
end

def shout greeting
greeting.upcase
end

def repeat greeting, how_many=2
long_greeting = ((greeting + " ") * how_many).chop
end

def start_of_word word, range
word[0,range]
end

def first_word phrase
phrase.split.first
end

end

44 changes: 44 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Calculator
#----------------------------------------------------------
def sum arr
# sum = 0 # my original
# arr.each { |a| sum += a } # my original
# sum # my original
# arr.inject(0){|sum,a| sum += a } #alt 1
arr.inject( 0, :+ ) #alt 2
end
#----------------------------------------------------------
def multiply arr, opt=0
#def multiply *arr #alt 1 splat args makes them optionsl # alt
product = 1 # my original
if arr.kind_of? Array
arr.each { |a| product *= a } # my original
product # my original
else # my original
product = arr * opt # my original
end # my original


# arr.flatten.inject(:*) #alt

end
#----------------------------------------------------------
def fac n
# product = 1 # my original = alt 1
# 1.upto(n) { |a| product = product * a } # my original
# 1.upto(n) { |a| product *= a } #alt 1
# product # my original = alt 1

(1..n).to_a.inject(1,:*) #alt 2

# return 1 if n==0 #alt3 alt a
# return 1 if n.zero? #alt3 alt b
# n * fac(n-1) #alt3 recursive; calls itself
end
#----------------------------------------------------------
def pow base, power
# expo = base**power # my original
base**power #alt
end
#----------------------------------------------------------
end
3 changes: 2 additions & 1 deletion week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "#{File.dirname(__FILE__)}/calculator"
# require "#{File.dirname(__FILE__)}/calculator"
require "./calculator.rb"

describe Calculator do

Expand Down
5 changes: 5 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
1a. A symbol, created and indicated by a preceding colon (e.g., :name) is the identifier of a string (e.g., name). What the string references (its value) can change, but the symbol doesn't change its value, and always returns the colon followed by the string name (e.g. in this example, it's value is always :name, even though the value of name may be "Pat" or change to something else).

2. What is the difference between a symbol and a string?
2a. A symbol is an identifier of a string, the string possibly being a variable name; in that case it's the name of the name, "the thing called :thing". It is created by putting a colon at the end of the string to create its symbol. The "value" of the symbol (what's returned when you show it, is the name (not the value) of the string it references. A string is an object that is a series of characters, usually identified by single or double quotes around it. It can be referenced by a variable, and that variable can, with the addition of a preceding colon, have a symbol that identifies it.

3. What is a block and how do I call a block?
3a. A block is a collection of code that either starts with "do" and ends with "end,"" or starts with an open brace "{" and ends with a closing brace "}". If a block starts with "def" it defines a named method consisting of that block, and it's called as a method by appending it to an object after a period ("."), if the method name is available in the range where the call is made, which can be done by "require"-ing the file it's in, then "include"-ing the name of the method or its parent module name.

4. How do I pass a block to a method? What is the method signature?
4a. You pass a block to a method by specifying 'def' followed by the name of the method, followed by the code that specifies the method, followed by 'end' to indicate the end of the method. The code between 'def' and 'end' (or for shorter coding, usually an opening { and a closing } ), is the block. When the name of the method is invoked to act on an object ("called"), it passes the block to the location of the call to act on the object according to the code in the block. This call may include passing some parameters for the method to use. The value returned to the method is the last value rendered before 'end' unless other steps are taken to return other values. The method signature is the name of the method plus any arguments (parameters) that are passed to the method's block from the calling invocation of the method name.

5. Where would you use regular expressions?
5a. You would use a regular expression to identify whether (or not), and where a particular string or string pattern occurs within another string. Once identified, the first occurrence of that string can be changed (with the method ".sub" to "sub"stitute another string), either returning a new string that has the change, or changed within the original string (with the method ".sub!" to force the substitution into the original string). If you want to change all occurrences of the string use the ".gsub"("global sub") method to generate a new string, or ".gsub!" to change the original string.
4 changes: 2 additions & 2 deletions week4/class_materials/odd_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def succ
new_val
end

def <=> (other)
@value <=> other.value
def <=> (other_odd_number)
@value <=> other_.value
end
end

17 changes: 16 additions & 1 deletion week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
Please Read:
Chapter 10 Basic Input and Output
Chapter 11 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
1a. Ruby reads files by invoking the open method on an object of the File class; e.g. File.open(filename), then doing some operation that does something with what's there, such as file.gets, which reads a line from the file (the string up to a CR or other defined line ending.

2. How would you output "Hello World!" to a file called my_output.txt?
2a. I would (and did to see if it worked) write and test the following code in .rb file:

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?
3a. The Directory class is used to instantiate objects that contain information about the contents of the file system. It's used to manipulate the contents of directories as objects, or move the current directory location between directories. Used in a rake file, it can use Ruby code to deliver commands to the command line as if they were being typed in manually, but as a batch file.

4. What is an IO object?
4a. An IO object is an instance of the IO class, a two way channel between Ruby code and a file, the console or some other external read/write resource, including some other resource on the Internet.

5. What is rake and what is it used for? What is a rake task?
5a. Rake is a domain specific language Ruby(like Rspec) gem that sends commands to the console (command line/terminal), either by typing them in the console or by running a file (named Rakefile as a default if no file name is specified) to send file creation/alteration tasks to the console. In general, it's used to build file structures needed for some program.

A rake task is defined by a method name preceded by a colon, invoked by using that method name after the rake command at the console line or some other place that has access to the file, rake tasks can also call other rake tasks.
47 changes: 47 additions & 0 deletions week4/homework/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#@module Worker
class Worker

=begin trivial case
def work no_of_times=1
yield
yield
yield
end

=end

#=begin named block, iterative case FAIL ==> returns value of no_of_times in every test
def self.work no_of_times=1
result = ""
no_of_times.times { result = yield if block_given? }
result
end
#=end

=begin named block, trivial case
def self.work no_of_times=1
no_of_times.times { return yield }
end
=end

=begin named block, trivial case
def self.work no_of_times=1
1.upto(no_of_times) { yield }
end
=end

end




# While this fulfills the literal requirement of the test conditions AS STATED including
# "executes a block 3 times and returns the result" (regardless of the parameter passed),
# it does seem to be a trivial non-general case. It should execute the block n times for any
# value of n passed.

# I have not figured out how to execute yield multiple times, and the things that seem to make
# sense all deliver different results, in part because they seem to redefine the block_given
# in the process of writing a new block.

# Tried &parameter naming as an argument, and n parameter.call s, but that didn't work either.
13 changes: 13 additions & 0 deletions week7/homework/features/step_definitions/pirate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PirateTranslator

def say arg1
@english = arg1
end

def translate
@result = ""
datastore = { 'Hello Friend' => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"}
@result = datastore[@english]
end

end # of class PirateTranslator
3 changes: 2 additions & 1 deletion week7/homework/features/step_definitions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Gangway /^I have a (\w+)$/ do |arg|
@translator = Kernel.const_get(arg).new
# @translator = Kernel.const_get(arg).new
@translator = PirateTranslator.new
end

Blimey /^I (\w+) '(.+)'$/ do |method, arg|
Expand Down
Loading