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

Week7 home work #92

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b494aaf
class work from week 1
reneedv Jan 10, 2014
201cd43
Merge pull request #16 from reneedv/master
reneedv Jan 10, 2014
ae4f39a
Merge pull request #17 from UWE-Ruby/master
reneedv Jan 12, 2014
9695a13
the rspec answers from week1
reneedv Jan 17, 2014
79563e6
Home work for week1 and week2.
Jan 23, 2014
775348b
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
Jan 23, 2014
1913bc1
Adding missed week1 homework files
Jan 23, 2014
ad03998
merge master into answers
reneedv Jan 24, 2014
5d74d1c
week1 answers
reneedv Jan 24, 2014
f16dc5d
Pulling in the week3 material
Jan 24, 2014
3f90898
week 2 hw answers
reneedv Jan 24, 2014
cba2595
week3 in-class work
reneedv Jan 24, 2014
2674309
Merge branch 'answers' of https://github.com/UWE-Ruby/RubyWinter2014
Jan 28, 2014
68c3943
Resolve conflicts of week1 & week2 homework due to merging answers b…
Jan 30, 2014
d479e14
week3 homework submission
Jan 30, 2014
5bea68a
Pulling week4 work
Feb 4, 2014
fb20c4d
Week4 home work
Feb 6, 2014
8dffe64
Pulling Week5 material(midterm)
Feb 7, 2014
8ed1aa5
Pulling week5 & week6 from upstream
Feb 15, 2014
a5b3e27
Pull Week7 from upstream
Feb 21, 2014
8d929da
week7 homework: pirate translator
Feb 22, 2014
704c18a
Answers for week7 questions
Feb 26, 2014
96ba073
Move pirate talk related work to different folder. Updated answers t…
Feb 27, 2014
ef0cfd5
Moving tictactoe to different folder
Feb 28, 2014
ca45503
Pull week 8 from upstream
Feb 28, 2014
67cfee4
First working version of TicTacToe!
Mar 8, 2014
69e175b
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
Mar 8, 2014
c4ee08e
Class room work of week 8
Mar 8, 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
2 changes: 2 additions & 0 deletions week1/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format nested
21 changes: 21 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
Object is an instance of a class having:
a. Unique internal state
b. Attributes that expose internal state to external world
c. Methods using which one can interact with Object and read/change the internal state.

2. What is a variable?
variable is a placeholder/identifier of data.

3. What is the difference between an object and a class?
class defines an object/class of objects.
When I create an object, I create an instance of a class that defines the object.

4. What is a String?

String is a sequence of characters represented as a single entity.

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

a. String.eql?(another_string) : Tests the equality of two strings.
b. String.clear : Empties the string
c. String.length : Returns the length of the string

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

A String literal can be defined in the following ways:

a. Using single-quote .i.e singleQuotedStr = 'This is a single quoted string'. This can also be done like singleQuotedStr = %q{This is a single quoted string}
b. Using double-quote .i.e doubleQuotedStr = "This is a double quoted string". This can also be done like doubleQuotedStr = %Q{This is a double quoted string}

Double-quoted string allows variables to be inserted into it and interpretd at run time. This is not possible with single-quoted strings.

22 changes: 14 additions & 8 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
describe String do
context "When a string is defined" do
before(:all) do
@my_string = "Renée is a fun teacher. Ruby is a really cool programming language"
@myString = "JayaPrakash likes programming and mystery novels"
end
it "should be able to count the charaters"
it "should be able to split on the . charater" do
pending
result = #do something with @my_string here
result.should have(2).items
it "should be able to count the charaters" do
(@myString.length).should eq 48
end
it "should be able to split on the ' ' charater" do
result = @myString.split(' ')
result.should have(6).items
end
it "should be able to give the encoding of the string" do
pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
(@myString.encoding).should_not eq nil
(@myString.encoding.to_s).should eq "UTF-8"
(@myString.encoding).should eq Encoding.find("UTF-8")
@myString = @myString.force_encoding(Encoding.find("ASCII-8BIT"))
(@myString.encoding).should eq Encoding.find("ASCII-8BIT")
(@myString.encoding).should_not eq Encoding.find("UTF-8")
(@myString.encoding == Encoding.find("UTF-8")).should eq false
end
end
end

12 changes: 6 additions & 6 deletions week1/ruby_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe "Playing With Ruby! " do
context 'when adding numbers' do
it "should add numbers" do
(2+2).should eq 4
end
end
describe "Playing With Ruby! " do
context 'when adding numbers' do
it "should add numbers" do
(2+2).should eq 4
end
end
end
2 changes: 2 additions & 0 deletions week2/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format nested
15 changes: 15 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?

Hash is indexed on objects where as Array is indexed on integers.

2. When would you use an Array over a Hash and vice versa?

I would use an Array to maintain a collection of similar objects, where as a Hash for dissimilar ones.

3. What is a module? Enumerable is a built in Ruby module, what is it?

Module defines a namespace, grouping together methods, constants and classes, which can
be mixed-in into other classes.

Enumerable is mixin that provides sorting, searching and traversal functionality.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?

A class can have only one parent. But we can inherit the behavior of more than one thing by using mixins.

5. What is the difference between a Module and a Class?

Module can't be instantiated like a Class. Multiple module can be included in a class, where as only
one class can be inherited by other.

35 changes: 35 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module SimonSays

def echo(str)
str
end

def shout(str)
str.upcase
end

def repeat(*args)
str = nil
count = 2
if (!args[0].nil?)
str = args[0]
end
if (!args[1].nil?)
count = args[1]
end
repeatedStr = String.new(str)
while (count > 1) do
count = count - 1
repeatedStr.concat(" ".concat(str))
end
repeatedStr
end

def start_of_word(str, count)
str[0...count]
end

def first_word(str)
str.split[0]
end
end
30 changes: 26 additions & 4 deletions week3/exercises/book.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
class Book
$global_hello = "hi there"

def pages

end
module Library
class Book
HELLO = "hello I shouln't change..."

attr_accessor :pages, :title

@@library_count = 0

def self.library_count
@@library_count
end

def initialize pages = 0, title = "N/A"
@pages = pages
@title = title
@@library_count += 1
end


def happy
$global_hello = "hello"
"There are #{@pages} happy pages in this book"
end


end
end
31 changes: 27 additions & 4 deletions week3/exercises/book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
require './book.rb'

describe Book do

it "should have a pages" do
book = Book.new
book.should respond_to "pages"

before :each do
@book = Book.new 542, "Programming Ruby"
end

context "::library_count" do
it "should tell us how many books are in our library" do
34233.times{ Book.new }
Book.library_count.should eq 34234
end
end

context "#pages" do
it "should have a pages" do
@book.should respond_to "pages"
end

it "should allow us to get the number of pages" do
@book.pages.should eq 542
end
end

context "#title" do
it "should let us read the title" do
@book.title.should eq "Programming Ruby"
end
end


end
6 changes: 6 additions & 0 deletions week3/exercises/human.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative 'named_thing'
require_relative 'other_thing'
class Human
include NamedThing
include OtherThing
end
6 changes: 6 additions & 0 deletions week3/exercises/monster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ def initialize(noc, legs, name="Monster", vul = [], dangers = [])
@dangers = dangers
@legs = legs
end

def attack! human
puts "hi from Monster"
super
end

end
5 changes: 5 additions & 0 deletions week3/exercises/other_thing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OtherThing
def say_name
"hello"
end
end
10 changes: 9 additions & 1 deletion week3/exercises/vampire.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require './monster.rb'
class Vampire < Monster
def initialize(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dangers=[:bites])
super(noc,legs,name,vul,dangers)
super
end



def attack! human
puts "hi from Vampire"
end


end
8 changes: 8 additions & 0 deletions week3/exercises/zombie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative 'named_thing'
class Zombie
include NamedThing

def say_name
"uuurrrggghhhh #{@name}"
end
end
2 changes: 2 additions & 0 deletions week3/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format nested
20 changes: 20 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Calculator

def sum input
input.inject(0,:+)
end

def pow base, exponent
base**exponent
end

def multiply *input
input.flatten.inject(1,:*)
end

def fac input
input = 1 if input == 0
(1..input).to_a.inject(:*)
end

end
12 changes: 12 additions & 0 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
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 floating point numbers" do
@calculator.sum([7.1,11.5]).should == 18.6
end

it "computes the sum of an array of many numbers" do
@calculator.sum([1,3,5,7,9]).should == 25
end

it "computes the sum of an array of with negative numbers numbers" do
@calculator.sum([1,3,5,7,-100]).should == -84
end
end

# Once the above tests pass,
Expand All @@ -31,6 +39,10 @@
@calculator.multiply(2,2).should eq 4
end

it "multiplies an empty array of numbers" do
@calculator.multiply([]).should eq 1
end

it "multiplies an array of numbers" do
@calculator.multiply([2,2]).should eq 4
end
Expand Down
29 changes: 29 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,39 @@ Please Read:

1. What is a symbol?

Symbol is a constant, that uniquely identifies string of characters (named thing). It is respresented as a colon followed by a string of characters.

2. What is the difference between a symbol and a string?

Since symbol is a constant its value/content can't be changed, where as a string content can be changed. Also a symbol is created only once irrespective of how many times we use it, where as a string is created multiple times if used at multiple places.

3. What is a block and how do I call a block?

Block is one or more lines of code enclosed in braces or keywords do and end. Block passed to a method can be invoked by using "yield" statement inside method definition.

4. How do I pass a block to a method? What is the method signature?

A block is passed to a method, by defining it immediately after the method invocation. If there are parameters to the method, then block should be defined after the parameters are specified.

def method1
...
end

method1 { "block that would be passed to method 1" }

def method2 (para1, para2)
...
end

method2 (a, b) do
"block that would" \
"be passed to method2"
end

5. Where would you use regular expressions?

Regular expression is a string of characters that defines a pattern. The patterns are used:

a. To test if a string or part of a string matches the pattern
b. To extract information from a string that matches the given pattern
c. To change (.i.e substitue/delete) sections within a string that matches the pattern
3 changes: 3 additions & 0 deletions week4/homework/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--color
--format nested
Loading