diff --git a/week1/homework/.DS_Store b/week1/homework/.DS_Store new file mode 100644 index 0000000..f96a5a9 Binary files /dev/null and b/week1/homework/.DS_Store differ diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 0adfd69..c5ee8da 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,22 +3,19 @@ Chapter 3 Classes, Objects, and Variables p.90-94 Strings 1. What is an object? -An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. +An object is everything that is manipulated, object start as instances of classes 2. What is a variable? -A variable is a name for a location in memory. It can contain, or point to, any type of object. +A variable is a reference to an object, used to track objects 3. What is the difference between an object and a class? -An object is an instance of a class, or a specific thing of that class's type in memory. The class is the specifics that are common to all things of that type. The classification of a concept or a thing is a class. A specific thing or concept of a class's type in memory is an object. For example: All books have titles (Class). This book's title is "Harry Potter and the Goblet of Fire" (Object). +A class is an object but contains methods, object of class Class 4. What is a String? -A string is how Ruby understands text. It is a collection of characters (Bytes), and can be created by making an instance of the String class (String.new) or as a string literal ("",'', %Q[]). +simple sequence of characters, normally holds printable data but not required. Object of class String 5. What are three messages that I can send to a string object? Hint: think methods -chomp! - removes newline characters, or the specified characters, from the end of a string -strip! - removes leading or trailing whitespace from a string -split - returns an array of strings made up of the original string separated on whitespace or the specified characters or regexp +squish, chomp, first 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? -Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same. - +single and double quoted, double quoted supports more escape sequences (such as \n) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 2f188f6..a30e367 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -16,11 +16,12 @@ @my_string.should have(@my_string.size).characters end it "should be able to split on the . charater" do - result = @my_string.split('.') - result.should have(2).items + result = @my_string.split(/\s*\.\s*/) + result.should have(2).items end it "should be able to give the encoding of the string" do - @my_string.encoding.should eq (Encoding.find("UTF-8")) + results = @my_string.encoding + results.should eq(Encoding.find("UTF-8")) end end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..8bf25b7 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,6 +3,23 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +<<<<<<< HEAD +Both are a collection of objects that are indexed, an array is indexed with integers while a hash is indexed with any object (symbols, strings, regular expressions, etc) + +2. When would you use an Array over a Hash and vice versa? +Use a hash when you want to obtain an object by referencing an object (example, items in a purse: lipstick, wallet, etc) while an array is an ordered collection that is referenced linearly (0, 1, 2, 3) + +3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a way of grouping together methods, classes, and constants. They provide a namespace, prevent name clashes, and supports the mixin facility. + +Enumerable: applies a function or operation to all items, since it runs through all objects you are able to use min/max/sort. Seems to be a simple way to evaluate all items instead of running through a loop. + +4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No, but you can use mixin modules to include subsets of information + +5. What is the difference between a Module and a Class? +A module contains methods and logic but does not retain information. A class contains objects while a module is not an object. +======= An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order. 2. When would you use an Array over a Hash and vice versa? @@ -16,3 +33,4 @@ No, multiple inheritance is not allowed in Ruby. You can include multiple module 5. What is the difference between a Module and a Class? A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. +>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index fa35ccd..6b0c44b 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,4 +1,29 @@ module SimonSays +<<<<<<< HEAD + def echo(message) + message + end + + def shout(message) + message.upcase + end + + def repeat(message,times_repeated=2) + a = [message]*times_repeated + a.join(" ") + end + + def start_of_word(message,char=1) + b = message.split(//) + b[0...char].join + end + + def first_word(message) + c = message.split(/ /).to_a + c[0] + end +end +======= def echo(st) st end @@ -20,3 +45,4 @@ def repeat(st, t=2) ([st]*t).join(' ') end end +>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c diff --git a/week2/homework/simon_says.rb.orig b/week2/homework/simon_says.rb.orig new file mode 100644 index 0000000..52f4d8d --- /dev/null +++ b/week2/homework/simon_says.rb.orig @@ -0,0 +1,47 @@ +module SimonSays +<<<<<<< HEAD + def echo(message) + message + end + + def shout(message) + message.upcase + end + + def repeat(message,times_repeated=2) + a = [message]*times_repeated + a.join(" ") + end + + def start_of_word(message,char=1) + b = message.split(//) + b[0...char].join + end + + def first_word(message) + c = message.split(/ /).to_a + c[0] + end +end +======= + def echo(st) + st + end + + def shout(st) + st.upcase + end + + def first_word(st) + st.split.first + end + + def start_of_word(st,i) + st[0...i] + end + + def repeat(st, t=2) + ([st]*t).join(' ') + end +end +>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c diff --git a/week3/exercises/inclass.rb b/week3/exercises/inclass.rb new file mode 100644 index 0000000..3cd4ca9 --- /dev/null +++ b/week3/exercises/inclass.rb @@ -0,0 +1,5 @@ +require /.named_thing.rb + +class Vampire + include NamedThing +end diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index f8916e3..fe3c703 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,4 +1,31 @@ class Calculator +<<<<<<< HEAD + def sum(num_array) + num_array.inject(0) do |total, value| + total + value + end + end + + def mult(x, y) + x*y + end + + def multa(num_array) + num_array.inject(1) do |total, value| + total * value + end + end + + def raiseby(x,y) + x**y + end + + def factorial(x) + (1..x).inject(1) do |total, value| + total*value + end + end +======= def sum(array) array.inject(0){|sum, x| sum +x} end @@ -21,4 +48,6 @@ def fac(n) def pow_fac(base=nil, p) (1..p).to_a.inject(1){|f,v| f *= base || v} end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 end + diff --git a/week3/homework/calculator_spec_hw.rb b/week3/homework/calculator_spec_hw.rb new file mode 100644 index 0000000..c99af45 --- /dev/null +++ b/week3/homework/calculator_spec_hw.rb @@ -0,0 +1,80 @@ +require "#{File.dirname(__FILE__)}/calculator" + +describe Calculator do + + before do + @calculator = Calculator.new + end + + describe "#sum" do + it "computes the sum of an empty array" do + @calculator.sum([]).should == 0 + end + + it "computes the sum of an array of one number" do + @calculator.sum([7]).should == 7 + end + + 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 many numbers" do + @calculator.sum([1,3,5,7,9]).should == 25 + end + end + + # Once the above tests pass, + # write tests and code for the following: + + describe "#mult" do + it "multiplies two numbers" do + @calculator.mult(7,2).should == 14 + end + + it "multiplies two numbers, same value" do + @calculator.mult(2,2).should == 4 + end + end + + describe "#multa" do + it "multiplies an array of numbers" do + @calculator.multa([1,2,3]).should == 6 + end + + it "multiplies an array of numbers" do + @calculator.multa([5,7,10]).should == 350 + end + end + + describe "#raiseby" do + it "raises one number to the power of another number" do + @calculator.raiseby(5,2).should == 25 + end + + it "raises one number to the power of another number" do + @calculator.raiseby(3,5).should == 243 + end + end + + # http://en.wikipedia.org/wiki/Factorial + describe "#factorial" do + it "computes the factorial of 0" do + @calculator.factorial(0).should == 1 + end + + it "computes the factorial of 1" do + @calculator.factorial(1).should == 1 + end + it "computes the factorial of 2" do + @calculator.factorial(2).should == 2 + end + it "computes the factorial of 5" do + @calculator.factorial(5).should == 120 + end + it "computes the factorial of 10" do + @calculator.factorial(10).should == 3628800 + end + end + +end diff --git a/week3/homework/questions_hw3.txt b/week3/homework/questions_hw3.txt new file mode 100644 index 0000000..5a103c7 --- /dev/null +++ b/week3/homework/questions_hw3.txt @@ -0,0 +1,18 @@ +1. What is a symbol? +A symbol is an identifier corresponding to a string of characters, often a name. + +2. What is the difference between a symbol and a string? +A symbol is an unchangable string. + +3. What is a block and how do I call a block? +A block is a bit of code between either braces or do/end, blocks are not objects. +A block can be called by using call, yield or []. +However, you can also call it by name.(arg) which maps to name.call(arg) + +4. How do I pass a block to a method? What is the method signature? +You pass a block to a method by listing the last parameter as a prefix with an ampersand. ex. def test(&block) +A method signature is the method name and all if its arguments. + +5. Where would you use regular expressions? +A regular expression is to identify a pattern in a string. +I imagine using regular expressions when finding data points based on unique ID format. \ No newline at end of file diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb index 67b7681..cf85b18 100644 --- a/week4/class_materials/timer.rb +++ b/week4/class_materials/timer.rb @@ -1,8 +1,18 @@ class Timer +<<<<<<< HEAD + def self.time_code(&my_code) + start_time = Time.now + my_code.call + end_time = Time.now + end_time - start_time + end +end +======= def self.time_code(n=1) start_time = Time.now n.times{yield} end_time = Time.now (end_time - start_time) / n.to_f end -end \ No newline at end of file +end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 diff --git a/week4/exercises/odd_number.rb b/week4/exercises/odd_number.rb new file mode 100644 index 0000000..c9e1e9f --- /dev/null +++ b/week4/exercises/odd_number.rb @@ -0,0 +1,21 @@ +class OddNumber + attr_reader :value + + def initialize(value) + @value = value + end + + def succ + new_val = nil + if + OddNumber.new(@value + 1) + else + OddNumber.new(@value+1) + end + new_val + end + + def <==>(other) + @value <==>other.value + end +end diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb index fad4e2f..c1e7ccd 100644 --- a/week4/exercises/worker.rb +++ b/week4/exercises/worker.rb @@ -1,5 +1,16 @@ class Worker +<<<<<<< HEAD + def self.work (n=1) + result = nil + n.times do + result = yield + end + result + end +end +======= def self.work(n=1) n.times.inject(nil){yield} end -end \ No newline at end of file +end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..fe2a5da 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,19 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +file.gets reads from a file +file.open("file name", "r") opens a file for reading + 2. How would you output "Hello World!" to a file called my_output.txt? +file.open("my_output.txt", "r+") +puts "Hello World!" +file.close + 3. What is the Directory class and what is it used for? +The Directory class represents directories in a file structure. Directories are used in many ways. You can open files using a directory, update the file, or create a new file. + 4. What is an IO object? +Part of IO class, An IO object is a bidirectional channel between a Ruby program and some external resource. Can be used for reading or writing to and from a file using methods + 5. What is rake and what is it used for? What is a rake task? +Rank groups tasks, a rank task is an action for a file. \ No newline at end of file diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..d4b125b --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,123 @@ +class TicTacToe + SYMBOLS = [:X, :O] + attr_accessor :player,:p_symbol, :comp, :comp_symbol,:board, :player_move, :current_player, :first_player, :winner + + def initialize (first_player=nil, first_symb=nil) + @first_player = first_player + pick_player_symbol(first_symb) + @board = {:A1=>" ", :A2=>" ",:A3=>" ", + :B1=>" ",:B2=>" ",:B3=>" ", + :C1=>" ",:C2=>" ",:C3=>" "} + end + + def pick_first_player + if @first_player.nil? + start = rand(0..1) + if start == 0 + @current_player="Computer" + else + @current_player=@player + end + else + if @first_player==:player + @current_player = @player + else + @current_player = "Computer" + end + end + end + + def current_player + if @current_player.nil? + pick_first_player + end + @current_player + end + + def pick_player_symbol(first_symbol) + if first_symbol.nil? + start = rand(0..1) + if start == 0 + @p_symbol=:X + else + @p_symbol=:O + end + else + @p_symbol = first_symbol + end + + if @p_symbol ==:X + @comp_symbol = :O + else + @comp_symbol = :X + end + end + + def welcome_player + "Welcome " + @player.to_s + end + + def player_symbol + @p_symbol + end + + def computer_symbol + @comp_symbol + end + + def indicate_palyer_turn + @player.to_s+"'s Move" + end + + def open_spots + @open_spots = @board.find_all{|space,value| value==" "} + end + + def computer_move + random_spot = rand(0..@open_spots.count) + spot = @open_spots[random_spot] + @board[spot[0]] = @comp_symbol + @current_player=@player + spot + end + + def current_state + @board.values.map(&:to_s) + end + + def get_player_move + gets.chomp + end + + def player_move + while move=get_player_move.to_sym do + if @board[move]==" " + @board[move]=@p_symbol + @current_player = "Computer" + break + end + end + move + end + + def determine_winner + win_scenarios = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + [:A1,:B2,:C3], + [:C1,:B2,:A3]] + + x_space = @board.map + + end + + + def player_won? + + end + +end