diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..7f45cc3 Binary files /dev/null and b/.DS_Store differ diff --git a/Karen b/Karen new file mode 160000 index 0000000..2a1c91c --- /dev/null +++ b/Karen @@ -0,0 +1 @@ +Subproject commit 2a1c91c002c3e21bb53466962ba06506ab497545 diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index 177358a..46d6279 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -11,6 +11,9 @@ Instructions for Mid-Term submission and Git Review (10pts): Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? + Three uses are: Curly brackets can replace 'do' and 'end'; they represent the beginning and end of a block + They represent a placeholder for data being held by a variable (i.e., #{}) + - What is a regular expression and what is a common use for them? - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? - Are these two statements equivalent? Why or Why Not? diff --git a/midterm/turkey.rb b/midterm/turkey.rb new file mode 100644 index 0000000..e69de29 diff --git a/midterm/wish_list.rb b/midterm/wish_list.rb new file mode 100644 index 0000000..1c5a6cd --- /dev/null +++ b/midterm/wish_list.rb @@ -0,0 +1,8 @@ +class WishList + def wishes + arraywish = ["Lamborghini", "Corn Starch and Water Moat", "Vegan Bacon Ice Cream", "Rubber Chicken", "Free Tickets to Ender's Game"] + en +arrayWish.map { |e| e, puts e } + end + +end \ No newline at end of file diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..3e1d926 --- /dev/null +++ b/test.rb @@ -0,0 +1,3 @@ +put "sdflkjsldf" |do| + +end \ No newline at end of file diff --git a/week1/.DS_Store b/week1/.DS_Store new file mode 100644 index 0000000..9efa86a Binary files /dev/null and b/week1/.DS_Store differ diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..7b1a766 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,25 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? + Objects are instances of a class. An object is defined by a class. A class is a blueprint from which individual objects are created. You typically define methods that let you access and manipulate the state of an object. + 2. What is a variable? + There are several different types of variables in Ruby: + Local Variable + Instance Variale + Global variable + Constant + + In general, a variable will vary or change, it is not fixed. 3. What is the difference between an object and a class? + An object is an instance of a class, while a class is a blueprint from which individual objects are created. 4. What is a String? +Ruby strings are sequences of characters. They normally hold printable characters. Strings are objects of class String. +Strings are often created using string literals - sequences of characters between delimiters. 5. What are three messages that I can send to a string object? Hint: think methods +a statement, expression, here documents 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..b8f6228 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,3 +1,4 @@ +#using ARGV # encoding: utf-8 # Please make these examples all pass @@ -14,13 +15,18 @@ 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 = str.count(@my_string) + result2 = @my_string.split(".") result.should have(2).items + return result + " " + result2 end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + + encoderesult = @my_string.encoding + encoderesult.encoding + encoderesult should eq Encoding.find("UTF-8") end end end - diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index a6b943d..83ed90e 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -4,9 +4,16 @@ class Book @@book_count = 0 +<<<<<<< HEAD + def initialize (title, pages) + @title = title + @pages = pages + end +======= def self.book_count @@book_count end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 def initialize title = "Not Set", page_count = 0 @@book_count += 1 @@ -24,4 +31,4 @@ def out_put_test puts @@book_count end -end \ No newline at end of file +end diff --git a/week2/exercises/book.rb.orig b/week2/exercises/book.rb.orig new file mode 100644 index 0000000..83ed90e --- /dev/null +++ b/week2/exercises/book.rb.orig @@ -0,0 +1,34 @@ +class Book + attr_accessor :title + attr_reader :page_count + + @@book_count = 0 + +<<<<<<< HEAD + def initialize (title, pages) + @title = title + @pages = pages + end +======= + def self.book_count + @@book_count + end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 + + def initialize title = "Not Set", page_count = 0 + @@book_count += 1 + @page_count = page_count + @title = title + end + + + def test + @test = "Hello!" + end + + def out_put_test + puts @test + puts @@book_count + end + +end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..f64daf5 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -2,6 +2,36 @@ describe Book do +<<<<<<< HEAD +# context "::new" do + + #it "should set some defaults" do + # Book.new.title.should eq "Not Set" + #end +#end + +it "should return the page count" do + @book.page_count.should eq "Page count is 200" + end + + before :each do + @book = Book.new("Harry Potter", 200) + end + it "should respond to title" do + @book.should respond_to "title" + end + + #context "#title" do + + #it "should allow me to set the title" do + # @book.title = "Snow Crash" + # @book.title should eq "Snow Crash" + # end + + #end + +end +======= context "::book_count" do @@ -46,4 +76,5 @@ end -end \ No newline at end of file +end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 diff --git a/week2/exercises/book_spec.rb.orig b/week2/exercises/book_spec.rb.orig new file mode 100644 index 0000000..f64daf5 --- /dev/null +++ b/week2/exercises/book_spec.rb.orig @@ -0,0 +1,80 @@ +require './book' + +describe Book do + +<<<<<<< HEAD +# context "::new" do + + #it "should set some defaults" do + # Book.new.title.should eq "Not Set" + #end +#end + +it "should return the page count" do + @book.page_count.should eq "Page count is 200" + end + + before :each do + @book = Book.new("Harry Potter", 200) + end + it "should respond to title" do + @book.should respond_to "title" + end + + #context "#title" do + + #it "should allow me to set the title" do + # @book.title = "Snow Crash" + # @book.title should eq "Snow Crash" + # end + + #end + +end +======= + + context "::book_count" do + + it "should count how many books have been created" do + Book.new + Book.new + Book.new + Book.book_count.should eq 3 + end + + end + + context "::new" do + + it "should set some defaults" do + Book.new.title.should eq "Not Set" + end + + it "should allow us to set the page count" do + book = Book.new "Harry Potter", 5 + book.page_count.should eq 5 + end + + end + + context "#title" do + + before :each do + @book = Book.new + end + + it "should have a 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 + + + + end + +end +>>>>>>> e040d45f81ce3c4ddfed6debf5b753b82d0e59f7 diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..dd9e514 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -1,13 +1,43 @@ -Please Read The Chapters on: -Containers, Blocks, and Iterators -Sharing Functionality: Inheritance, Modules, and Mixins +Please Read The Chapters on: +Containers, Blocks, and Iterators +Sharing Functionality: Inheritance, Modules, and Mixins + +1. What is the difference between a Hash and an Array? + Arrays are ordered, integer-indexed collections of any object. Each object reference occupies a position in the array, identified by a non-negative integer-index. Hashes, like arrays, are indexed collections of object references; however, while arrays are indexed with integers, you index a hash with objects of any type: symbols, strings, regular expressions etc. + When storing a value in a hash, you store the key and the entry to be stored with the key. + +2. When would you use an Array over a Hash and vice versa? + An example re: when you would use a hash would be: If you wanted to map musical instruments to their orchestral sections, you would use a hash: + +instSection = { + 'cello' => 'string', + 'clarinet' => 'woodwind', + 'drum' => 'percussion', + 'oboe' => 'woodwind', + 'trumpet' => 'brass', + 'violin' => 'string' +} + + An example re: when you would use an array would be: + If you wanted to iterate single list of items or pull them out and list them as needed, you could use an array: + + a = %w{ cat dog bird lion deer} + a[0] >> "cat" + a[3] >> "lion" + + + +3. What is a module? Enumerable is a built in Ruby module, what is it? + modules are a way of grouping together methods, classes and constants. Modules give you two major benefits: + They prevent name clashes; modules support the min facility. + + modules provide a namespace, a sandbox in which methods and constants can play without being stepped on by other methods and constants. Modules essentially eliminate the need for inheritance, providing a facility called a mixin. + + The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. + +4. Can you inherit more than one thing in Ruby? How could you get around this problem? + No, multiple inheritance is not allowed in Ruby. You could get around this issue by creating a module and include it within a class. Once included within a class, you have access to all of the module's instance methods in the class as well. They get mixed in. Mixed-in modules behave as superclasses. + +5. What is the difference between a Module and a Class? + While a class has instances, a module cannot. A module can't have instances because a module is not a class. However, you can include a module within a class definition. -1. What is the difference between a Hash and an Array? - -2. When would you use an Array over a Hash and vice versa? - -3. What is a module? Enumerable is a built in Ruby module, what is it? - -4. Can you inherit more than one thing in Ruby? How could you get around this problem? - -5. What is the difference between a Module and a Class? diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..fc1a91b --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,30 @@ +module SimonSays + + #methods + + def echo (message) + @message = message + end + + def shout(message_to_shout) + @message_to_shout = message_to_shout.upcase + end + + def repeat(word_to_repeat, times_to_repeat = 2) + @word_to_repeat = word_to_repeat + (@word_to_repeat + " ") * times_to_repeat + end + + def start_of_word(return_letters, number_of_letters) + @return_letters = return_letters + @number_of_letters = number_of_letters + @chopped_string = return_letters.slice(0..(@number_of_letters -1)) + end + + def first_word(word_to_break) + @word_to_break = word_to_break + @result = @word_to_break.split + @result[0] + end + +end \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..7dde2d1 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,34 @@ +class Calculator + + + def sum array +# array.inject(0){|memo, n| memo + n} + array.inject(0, :+) + end + +# def sum input +# result = 0 +# input.each do | i | +# result += i +# end +# result + + # def multiply (array_prod) + # @array_prod = array_prod + # @product = product + # @product = array_prod.inject{|memo, n| memo * 2} + + # end + + def multiply *args + args.flatten.inject(:*) + end + + def pow (num_val, power) + num_val ** power + end + + def fac(num) + (1..num).to_a.inject (:*) + end + end \ No newline at end of file diff --git a/week3/homework/calculator.rb_1028330.rb b/week3/homework/calculator.rb_1028330.rb new file mode 100644 index 0000000..cd7291e --- /dev/null +++ b/week3/homework/calculator.rb_1028330.rb @@ -0,0 +1,28 @@ +class Calculator + attr_accessor :array, :array_prod, :product, :num, :num_val, :power + + def sum (array=0) + @array = array + array.inject{|memo, n| memo + n} + end + + + def multiply (array_prod) + @array_prod = array_prod + @product = product + @product = array_prod.inject{|memo, n| memo * 2} + + end + + def fac(num) + @num = num + (1..num).inject (:*) + end + + def pow (num_val, power) + @num_val = num_val + @power = power + num_val ** power + end + + end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..f291beb 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,25 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A label that is used to identify a piece of data. A symbol is stored in memory one time. These work well inside hashes. This helps conserve memory. 2. What is the difference between a symbol and a string? +A symbol is stored in memory one time while a string is stored in memory each time it is called. Every time it creates a string a new Ruby object is created. 3. What is a block and how do I call a block? + a block is code we want to be iterated each time through the loop denoted by curly braces or do and end. An iterator invokes a block of code repeatedly, including the block variable until we end our block of code. + +You can call the block by using yield after the iterator within the block. This invokes the code in the block. When the block exits, control picks back up immediately after the yield. + +In the definition of the block, the argument list appears between vertical bars. In this instance, the variable f receives the value passed to the yield, so the block prints successive members of the series. A block is always invoked from a function with the same name as that of the block. + +A Ruby iterator can invoke a block of code. 4. How do I pass a block to a method? What is the method signature? + You can invoke it using the yield statement. All methods that use blocks are iterators. Blocks are enclosed by curly braces. 5. Where would you use regular expressions? + +You would use regular expressions when you are trying to match or find a set of strings or characters that match a specific pattern. + +A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. diff --git a/week4/exercises/.DS_Store b/week4/exercises/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/week4/exercises/.DS_Store differ diff --git a/week4/exercises/in_class/monster.rb b/week4/exercises/in_class/monster.rb new file mode 100644 index 0000000..d7e580e --- /dev/null +++ b/week4/exercises/in_class/monster.rb @@ -0,0 +1,15 @@ +require './named_thing.rb' + +Class Monster +include NamedThing +attr_accessor :name, :legs, :dangers, :vulnerabilities, :nocturnal + +def initialize legs, nocturnal, name="Monster", vulnerabilities, dangers=[] + @legs = legs + @nocturnal = nocturnal + @dangers = dangers + @vulnerabilities = vulnerabilities + super name + end + +end \ No newline at end of file diff --git a/week4/exercises/in_class/named_thing.rb b/week4/exercises/in_class/named_thing.rb new file mode 100644 index 0000000..d4d59c2 --- /dev/null +++ b/week4/exercises/in_class/named_thing.rb @@ -0,0 +1,6 @@ +module NamedThing + +attr_accessor :name + +def initialize name +@name = name \ No newline at end of file diff --git a/week4/exercises/in_class/vampire.rb b/week4/exercises/in_class/vampire.rb new file mode 100644 index 0000000..f7b1ea2 --- /dev/null +++ b/week4/exercises/in_class/vampire.rb @@ -0,0 +1,8 @@ +require './monster.rb' + +Class Vampire < Monster + + def initialize legs = 2, nocturnal, name = "vampire", dangers =[:bites], vulnerabilities = [:sunlight, :garlic] + super legs, nocturnal, name="monsters", dangers, vulnerabilities + end +end diff --git a/week4/homework/Rakefile.rb b/week4/homework/Rakefile.rb new file mode 100644 index 0000000..571c0b8 --- /dev/null +++ b/week4/homework/Rakefile.rb @@ -0,0 +1,13 @@ +task :default => [:hello_world] + +#can have multiple detaults +#rake is task management + +desc "This outputs hello world!" +task :hello_world do + puts test +end + +def test + "tequila" +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..ffe28ae 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,17 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + 'gets' reads a line from standard input and 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") { |f| f.puts "Hello World!" } + 3. What is the Directory class and what is it used for? + Allows you to manipulate directories as required via the command line. This is assists with creating new paths and directories as well as changing + and/or removing current directories. + 4. What is an IO object? + It is a bidirectional channel between a Ruby program and and an external resource. You simply write to it and read to it. A single IO object contains both a read pipe and a write pipe. + 5. What is rake and what is it used for? What is a rake task? + Rake allows you to automate items, modify files, move data. Both file tasks and regular tasks exist with Rake. Task is a keywod that introduces + a task definition. Rake is a build language. It is a Domain Specific Language. It allows you to specify tasks and describe dependencies as well as to group tasks in a namespace. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..a63aec4 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,11 @@ +class Worker + def self.work n = 1 + # results = nil + # n.times {results = yield} + # results + #n.times.inject (nil) {|results, i| yield} + n.times.inject(nil) {yield} + end + + +end \ No newline at end of file diff --git a/week7/.DS_Store b/week7/.DS_Store new file mode 100644 index 0000000..2f1c263 Binary files /dev/null and b/week7/.DS_Store differ diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..7423183 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,22 @@ +class PirateTranslator + +PIRATE_WORDS = { + Hello_Friend: "Ahoy Matey" +} + def say something + @said = something + end + + def translate + priate_lookup (@said) + "\n shiber me Timbers" + end + + #private method + private + + def pirate_lookup said + key = said.gsub(' ','_').downcase.to_sym + PIRATE_WORDS[key] + end + +end \ 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..5db7028 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,103 @@ + +class TicTacToe + attr_accessor :player, :computer, :board + SYMBOLS = [:X, :O] + + +def initialize (player) + @player = player + puts board + end + +def welcome_player + return "Welcome #{@player}" +end + +def player_symbol + @player_symbol[:player] +end + +def computer_symbol + @player_symbol[:computer] +end + +def current_player + @current_player = "Renee" || @current_player = "Computer" +end + +def current_state + row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row2.size+"\n"+ + row3 + "-"*row3.size+"\n"+ + "_____________" + end + + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def get_player_move + gets.chomp + end + +def board + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + end + + + +#end class +end + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..a5e36af 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,20 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method. + + Method_missing is in part a safety net: It gives you a way to intercept unanswerable messages and handle them. It prevents an exception from being thrown if the method called is missing. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + Singleton methods are specific to a particular object. When a Singleton method is defined for an object, Ruby creates an annonymous class and defines the given method in that class. The annonymous class is sometimes called a singleton class or eigenclass. + 3. When would you use DuckTypeing? How would you use it to improve your code? + We could use ducktyping to add a customers name to a file. I can pass any object to a method foo(a) that calls a.bar as long as a.responds_to?(:bar). You don't need a type in order to invoke an existing method on an object - if a method is defined on it, you can invoke it. Essentially, it gives you more flexiblity in 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? + Class methods: you define class methods in one class and then use them in subclasses of that class. Instance Methods: object's "methods" are the instance methods of its class, plus the instance methods of the superclass, the superclass of the superclass, etc. + + Class_eval sets up the environment as if you were in the body of a class definition, so method definitions will define instance methods; instance_eval, when called on a class, acts as if you were working inside the singleton class of self. Therefore, any methods defined will become class methods. So, class_defines instance methods and instance_eval defines class methods. + 5. What is the difference between a singleton class and a singleton method? + Singleton Class has only one instance. It will inherit both instance and singleton methods. Singleton methods are only available for a specific object. \ No newline at end of file diff --git a/week8/class_materials/couch.rb b/week8/class_materials/couch.rb index a919c51..ae9d31c 100644 --- a/week8/class_materials/couch.rb +++ b/week8/class_materials/couch.rb @@ -3,7 +3,17 @@ def initialize(pillows, cushions, dogs) @pillows = pillows @cushions = cushions @dogs = dogs - end + + def colors (color_pillow, color_couch) + @color_pillow = color_pillow + @color_couch = color_couch + end + + [:color_pillow, :color_couch].each do |color| + define_method("What color are_#{color}") do + instance_variable_get("@#{color}").count + end + end [:pillows, :cushions, :dogs].each do |s| define_method("how_many_#{s}") do @@ -11,6 +21,25 @@ def initialize(pillows, cushions, dogs) end end + def how_many_pillows + @pillows.size + end + + def how_many_cushions + @cushions.size + end + + def how_many_dogs + @dogs.size + end + + [:pillows, :cushions, :dogs].each do |var| + define_method("how_many_#{var}") do + instance_variable_get("@#{var}").size + end + end +end + # def to_str # "I am a Couch" # end