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

Assignment #107

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

1. What is an object?
Every object in Ruby is an instance of a particular class, which is itself an object.

2. What is a variable?
A variable refers to an object.

3. What is the difference between an object and a class?
As explained by David Black in his book "The Well-Grounded Rubyist", classes define clusters of
behavior or functionality and each object is an instance of a class. Objects can acquire
methods and behaviors not defined in their class.

4. What is a String?
A string is a sequence of characters. Typically, a string holds printable characters.
It can also hold binary data.

5. What are three messages that I can send to a string object? Hint: think methods
length - returns the number of characters in a string
capitalize - returns a copy of the string in which the first character appears in uppercase and the rest in lowercase
reverse - returns a new string in which the characters appear in reverse order

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
Double quotes and single quotes. The difference is you cannot do string interpolation in
single-quoted strings. Also, more escape sequences are supported by double-quoted strings.

*Resources used for this assignment: "Programming Ruby 1.9 & 2.0", "The Well-Grounded Rubyist"

9 changes: 5 additions & 4 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -12,14 +12,15 @@
before(:all) do
@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.should have(66).characters
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"))'
@my_string.encoding.name.should eq "UTF-8"
end
end
end
14 changes: 14 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,11 +3,25 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
An array is indexed with integers only, while a hash can be indexed
with objects of any type.

2. When would you use an Array over a Hash and vice versa?
A typical use of the hash, according to the book The Well-Grounded Rubyist,
is storing strings along with their abbreviations. For example, state names and their
abbreviations.

3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules are a way of bundling methods, classes and constants together.
Enumerable is a standard mixin. When Enumerable is mixed into a class, the class
has to define an each instance method. Objects of that class are then able to
to call any instance method defined in the Enumerable module.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
With Ruby being a single-inheritance language, a Ruby class has just one direct parent.
However, classes can include the functionality of any number of mixins.

5. What is the difference between a Module and a Class?
Classes have instances while modules do not.

Info source: "Programming Ruby" and "The Well-Grounded Rubyist"
27 changes: 27 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SimonSays
def echo(string)
return string
end


def shout(string)
return string.upcase
end

def repeat(string, num = 2)
a = (string + " ") * num
return a.rstrip
end

def start_of_word(string, num)
string.split(//)
return string[0..(num - 1)]
end

def first_word(string)
b = string.split
return b[0]
end

end

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

def sum x=[]
x.inject(0) {|sum, num| sum + num}
end

def multiply b
b.inject(:*)
end


def pow num1, num2
num1 ** num2
end

def fac num
if num == 0
return 1
else
(1..num).inject(1) {|product, num| product * num}
end
end
end
8 changes: 8 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -5,11 +5,19 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
A symbol is an identifier that corresponds to a string of characters.

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

3. What is a block and how do I call a block?
A block is a batch of code placed between either braces or the keywords
"do" and "end."

4. How do I pass a block to a method? What is the method signature?
Using the yield statement, a block may be invoked within a method.

5. Where would you use regular expressions?
Regular expressions are used for pattern matching and substitution.

Info source: "Programming Ruby"
13 changes: 13 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,12 +3,25 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
Gets reads from files specified on the command line.
File.gets reads a line from the file object file.


2. How would you output "Hello World!" to a file called my_output.txt?
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?
Objects of class Dir are directory streams representing directories in the underlying file system.
They provide ways to list directories and their contents.

4. What is an IO object?
An IO object is a bidirectional channel between a Ruby program and an external resource.

5. What is rake and what is it used for? What is a rake task?
Rake is a Ruby gem with capabilities similar to make. Users can specify tasks with prerequisites.
A rake task is the basic unit of work in a Rakefile.

*Info source: "Programming Ruby", http://rake.rubyforge.org/
10 changes: 10 additions & 0 deletions week4/homework/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#I tried but failed to come up with something for the last test.

class Worker
def self.work *n
yield n
end


end

14 changes: 14 additions & 0 deletions week7/homework/features/step_definitions/pirate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class PirateTranslator
def say (word)
@word = word
end

def translate
self.send("split")
end

def split

end

end
Original file line number Diff line number Diff line change
@@ -33,9 +33,10 @@
@game.player.should eq "Renee"
end


Then /^the computer prints "(.*?)"$/ do |arg1|
@game.should_receive(:puts).with(arg1)
@game.indicate_palyer_turn
@game.indicate_player_turn
end

Then /^waits for my input of "(.*?)"$/ do |arg1|
107 changes: 107 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@


class TicTacToe

SYMBOLS = [:X, :O]

@board = {
:A1 => " ", :A2 => " ", :A3 => " ",
:B1 => " ", :B2 => " ", :B3 => " ",
:C1 => " ", :C2 => " ", :C3 => " "
}

attr_accessor :player
attr_reader :player_symbol, :computer_symbol

def initialize(*args)
@current_player = rand([@player, "Computer"].length)
if @current_player == 0
@current_player = @player
else
@current_player = "Computer"
end
@player_symbol = SYMBOLS[rand(SYMBOLS.length)]
if @player_symbol == :X
@computer_symbol = :O
else
@computer_symbol = :X

end
end



def player= name
@player = name
end

def welcome_player
"Welcome #{@player}"
end

def current_player
if @current_player = @player
return @player
else
return "Computer"
end
end

def player_symbol
@player_symbol
end

def computer_symbol
@computer_symbol
end

def indicate_player_turn
puts "#{@player}'s Move:"
end



def get_player_move
move = gets.chomp

end


def open_spots
a = @board.select{|key, value| value == ""}
@open_spots = a.keys

end

def player_move
end

def computer_move
@com_move = @open_spots[rand(@open_spots.length)]
@com_move = @com_move.to_sym
@current_player = @player
end

def determine_winner
end



end





#Given /^it is the computer's turn$/ do
# @game = TicTacToe.new(:computer, :O)
# @game.current_player.should eq "Computer"
#end

#Then /^the computer randomly chooses an open position for its move$/ do
# open_spots = @game.open_spots
# @com_move = @game.computer_move
# open_spots.should include(@com_move)
#end


2 changes: 1 addition & 1 deletion week7/homework/play_game.rb
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
when "Computer"
@game.computer_move
when @game.player
@game.indicate_palyer_turn
@game.indicate_player_turn
@game.player_move
end
puts @game.current_state
20 changes: 19 additions & 1 deletion week7/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,7 +3,25 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming

Questions:
1. What is method_missing and how can it be used?
2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?
The method_missing method is executed whenever an object receives a message that's not in the object's method-lookup path. One can override this method either on a singleton basis or in the object's class or
one of the class' ancestors.

2. What is and Eigenclass and what is it used for? Where Do Singleton methods live
Eigenclass is another term for an anonymous class or singleton class. Singleton methods live in the object's
singleton class.

3. When would you use DuckTypeing? How would you use it to improve your code?


4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval?
A class method is a singleton method defined on a class object. You send a message to the class, instead of instances of the class.
An instance method can be used by all instances of the class.

Class_eval sets things up as though you were in the body of a class definition, so method definitions will define instance methods.
Calling instance_eval on a class acts as though you were in the singleton class of self, so methods you define become class methods.

5. What is the difference between a singleton class and a singleton method?
A singleton method is a method specific to a particular object. An object's singleton method resides in the object's singleton
class.

*Info source: "Programming Ruby", "The Well-Grounded Rubyist"