Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished week 3 homework + week 4 homework + week 7 homework + final #41

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3f89d08
Merge pull request #1 from UWE-Ruby/master
tomun Oct 4, 2014
ba263e1
Answered homework questions
tomun Oct 10, 2014
9677709
Finished rspec homework
tomun Oct 10, 2014
82d44b8
Merge pull request #2 from UWE-Ruby/master
tomun Oct 14, 2014
a195508
Week 2 homeworkWeek 2 homework
tomun Oct 15, 2014
c56bed6
Merge pull request #3 from UWE-Ruby/master
tomun Oct 17, 2014
67418e4
Finished week 3 homework
tomun Oct 19, 2014
117dbda
Week 3 homework
tomun Oct 19, 2014
e4ec315
Merge pull request #4 from UWE-Ruby/master
tomun Oct 24, 2014
411e6df
Update questions.txt
tomun Oct 28, 2014
adb265a
Finished week 4 homework
tomun Oct 28, 2014
c134961
Merge pull request #5 from UWE-Ruby/master
tomun Oct 31, 2014
c8c3f84
Merge pull request #6 from UWE-Ruby/master
tomun Nov 8, 2014
e99c830
Merge pull request #7 from UWE-Ruby/master
tomun Nov 14, 2014
5b5a358
Finished pirate translator
tomun Nov 21, 2014
088d99e
Got 3/4 of tic-tac-toe passing
tomun Nov 21, 2014
264dcd0
Answered questions
tomun Nov 21, 2014
02c995e
Merge pull request #8 from UWE-Ruby/master
tomun Nov 21, 2014
19caf7d
All but two tests passing
tomun Dec 2, 2014
c66da24
Game plays, 2 tests are not passing
tomun Dec 2, 2014
b6ff665
More work on tic tac toe
tomun Dec 2, 2014
2d3ed06
Checking in some class excercise work
tomun Dec 2, 2014
34880c7
Everything is passing in Cucumber and the game plays correctly
tomun Dec 3, 2014
172ded1
Merge pull request #9 from UWE-Ruby/master
tomun Dec 5, 2014
725c05e
Merge pull request #10 from UWE-Ruby/master
tomun Dec 28, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
An object is structure of memory with associated methods that act on the members of the structure.

2. What is a variable?
A variable is a symbolic reference to an object

3. What is the difference between an object and a class?
A class is a definition of an object, an object is a specific instance of a class

4. What is a String?
A string is a sequence of characters

5. What are three messages that I can send to a string object? Hint: think methods
squeeze, split, chomp

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
strings are delimited by special characters at the beginning and end of the string. Ruby has several forms of delimeters: single quotes, double quotes, %{} where { and } may be any pair of braces ({,[,etc). There are also here documents which begin <<ANY_END_DELIMETER string ANY_END_DELIMETER. the difference between single and double quote strings is that single quote strings only allow \\ and \' escapes where double quotes allows a much greater variety of escape sequences including \n, and #{ code }.

'])
11 changes: 5 additions & 6 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
@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
@my_string.length.should > 0
end

it "should be able to split on the . charater" do
pending
result = #do something with @my_string here
result = @my_string.split('.')
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
@my_string.encoding.should eq (Encoding.find("UTF-8"))
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
A hash is a collection of key-value pairs that can be accessed by unique keys. An array is a collection that is accessed by numeric index.

2. When would you use an Array over a Hash and vice versa?
An array can access its elements instanenously by its index. If a particular object needs to be access by a key value, for example by name, then a Hash collection who's key is a name string would be ideal. Arrays are useful as stacks or queues. Hash's are useful when you need rapid access by arbitrary keys.

3. What is a module? Enumerable is a built in Ruby module, what is it?
A module is a way to provide a namespace for a group of methods, classes, and constants. As a namspace, a module helps prevent collisions with other unrelated but similary named methods, classes, and constants. A module also lets you mixin module methods into classes.

The Enumerable module defines methods that iterate over collections, such as map, include?, find_all?. One can define their own collection class by defining an iterator method called 'each' which returns elements in the collection. That class may include or 'mixin' the Enumerator module and the new collection class now has all the standard Enumerable methods.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No, a class can only inherit from a single base class. You can, however, mixin as many modules as you want within a class.

5. What is the difference between a Module and a Class?
A module just defines a namespace and grouping of a set of methods, classes, and constants. A class defines a type of object. A class can be instaniated into an object, modules cannot. Classes can inherit from other classes, modules cannot. A module can be included in classes and other modules.
23 changes: 23 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module SimonSays

def echo(word)
word
end

def shout(word)
word.upcase
end

def repeat(word, count = 2)
((word + " ") * count).rstrip
end

def start_of_word(word, count)
word[0, count]
end

def first_word(sentence)
sentence.split(" ").first
end

end
65 changes: 65 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Calculator

def pow power, number
p = 1
number.times { p *= power }
p
end

def sum array
s = 0
array.each do |value|
s += value
end
s
end

def multiply *args
if args[0].class == Array
multiplyArray args[0]
else
multiplyArray args
end
end

def multiplyArray array
m = 1
array.each do |value|
m *= value
end
m
end

def multiply_Alternative *args
if args[0].class == Array
array = args[0]
else
array = args
end
m = 1
array.each do |value|
m *= value
end
m
end

def multiply_Alternative2 numOrArray, num = 0
m = 1
if numOrArray.class == Array
numOrArray.each do |value|
m *= value
end
else
m = numOrArray * num
end
m
end

def fac n
f = 1
(1..n).each do |a|
f *= a
end
f
end
end
9 changes: 9 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
A symbol is an identifier that is a string of characters. A particular name or string will always generate the same symbol, so they are useful as unique keys in code.

2. What is the difference between a symbol and a string?
A symbol is used as an indentifier in code, a string is a variable to hold user data, results, etc.

3. What is a block and how do I call a block?
A block is a segment of executable code, like any function but it doesn't have a name -- its an anonymous function. A block can be assigned to a variable or passed to a function as a parameter. Blocks can be called using the 'call' method on a Proc object.

4. How do I pass a block to a method? What is the method signature?
A block can be passed to a method using a variable in any argument, or the if the last parameter is declard with an &, then an inline block can appear immediately after the function calls other regular parameters (if any). For example:
def foo &block
end
...
foo do ... end

5. Where would you use regular expressions?
A regular expression can be used to test to see if a particular pattern exists in a string. It can also be used to extract pattern matches from strings or perform substitutions in strings.
8 changes: 8 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
Ruby reads files similar to how it can get steams from the console.
If a filename was passed to ruby after the ruby script itself, then the Kernel 'gets' will lines from the file instead of the console. A File object can also be created with a file path (File.new("filename", "r")) and then #gets can be called on the file object to get lines of text, or #read to read an arbitrary number of bytes from the file.

2. How would you output "Hello World!" to a file called my_output.txt?
f = File.new("my_output.txt", "w")
f.puts("Hello World!")
f.close

3. What is the Directory class and what is it used for?
The Dir class lets ruby inspect directories in the underlying file system. For example Dir.entries("/") returns an array containing all the filename of the files in the root directory of the system.

4. What is an IO object?
The IO class is the basis for input and output in ruby. It is the base class of File and the socket classes like TCPSocket and UDPSocket. It defines the methods such as #gets, #puts, #read and #print that read or write steams of characters.

5. What is rake and what is it used for? What is a rake task?
Rake is a make file like build program. Rakefiles let you define build tasks and describe dependencies between files. Rakefiles are a domain specific language in ruby syntax. a rake task is a block of ruby code that rake runs when a rule or dependency triggers it to be run.
11 changes: 11 additions & 0 deletions week4/homework/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Worker

def self.work t = 1
result = nil
t.times do
result = yield
end
result
end

end
24 changes: 24 additions & 0 deletions week5/exercises/rakefile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

task :name_students do
f = File.new "names"

f.each do |name|
puts name
end
end

task :create_class_dir do
Dir.mkdir("class")
end

task :create_student_dirs => [:create_class_dir] do
f = File.new "names"

Dir.chdir("class")

f.each do |name|
Dir.mkdir name.chop
end

Dir.chdir("..")
end
1 change: 1 addition & 0 deletions week6/homework/tomun_test_gem/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color --format documentation
5 changes: 5 additions & 0 deletions week6/homework/tomun_test_gem/RakeFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new('spec')

task :default => :spec
5 changes: 5 additions & 0 deletions week6/homework/tomun_test_gem/bin/tomun_test_gem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require_relative "../lib/tomun_test_gem.rb"

run
3 changes: 3 additions & 0 deletions week6/homework/tomun_test_gem/lib/tomun_test_gem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def run
puts "Tomun's test gem"
end
8 changes: 8 additions & 0 deletions week6/homework/tomun_test_gem/spec/tomun_test_gem_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../lib/tomun_test_gem'

describe "Test Gem" do

it "Runs" do
expect { run }.to output("Tomun's test gem\n").to_stdout
end
end
Binary file not shown.
13 changes: 13 additions & 0 deletions week6/homework/tomun_test_gem/tomun_test_gem.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Gem::Specification.new do |s|
s.name = 'tomun_test_gem'
s.version = '1.0.0'
s.date = '2014-11-07'
s.summary = "Tomun's Test Gem"
s.description = "A gem to for Week 6 homework"
s.authors = ["Tom Underhill"]
s.email = '[email protected]'
s.homepage = 'http://www.tomunderhill.cu.cc'
s.licenses = ['MIT']
s.files = ["lib/tomun_test_gem.rb"]
s.executables = ["tomun_test_gem"]
end
19 changes: 19 additions & 0 deletions week7/exercises/features/step_definitions/converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Converter
attr_accessor :value
attr_accessor :type

def initialize
end

def convert
# [°C] = ([°F] - 32) × 5/9
# [°F] = [°C] × 9/5 + 32

if type == :fahrenheit
value * 9/5 + 32
else
(value - 32) * 5/9
end
end

end
17 changes: 17 additions & 0 deletions week7/exercises/features/step_definitions/converter_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Given(/^I have entered (\d+) into the converter$/) do |arg1|
@converter = Converter.new
@converter.value = arg1.to_f
end

Given(/^I set the type to Fahrenheit$/) do
@converter.type = :fahrenheit
end

When(/^I press convert$/) do
@result = @converter.convert
end

Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2|
@converter.value == "#{arg1}.#{arg2}"
end
12 changes: 12 additions & 0 deletions week7/homework/features/step_definitions/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class PirateTranslator
def say string
@message = string
end

def translate
pirate = @message
pirate.gsub!("Hello", "Ahoy");
pirate.gsub!("Friend", "Matey");
pirate << "\n Shiber Me Timbers You Scurvey Dogs!!"
end
end
Loading