diff --git a/week1/exercises/moon_spec.rb b/week1/exercises/moon_spec.rb new file mode 100644 index 0000000..ca0218b --- /dev/null +++ b/week1/exercises/moon_spec.rb @@ -0,0 +1,7 @@ +describe "MoonPlugin" do + context "When the moon is full" do + it "should tell us it's a full moon" do + "Renee".should include("ee") + end + end +end diff --git a/week1/exercises/roster.txt~ b/week1/exercises/roster.txt~ new file mode 100644 index 0000000..552e9eb --- /dev/null +++ b/week1/exercises/roster.txt~ @@ -0,0 +1,2 @@ +name, email, github, twitter, hipchat +Renee D, renee@nird.us, reneedv, @gigglegirl4e, renee.devoursney@gmail.com diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index f4c2f0b..0d0798a 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -89,7 +89,12 @@ it "should check basic spelling" do "Field".should include('ie') - end + it "should count the characters in your name" + "Chris".should have(5).characters + it "should check basic math" + + it "should check basic spelling" + end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 0adfd69..139b03b 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,22 +3,18 @@ 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 an instantiated class. 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 a stored value or object. 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 the definition of a type of object - basically, a blueprint. Many objects can be created from + one class, that will all share the same structure and functionality, but can hold discrete states and values. 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[]). - + A string is simply a chain of characters (or... a 'string' of characters... clever). In the simplest of terms, + it is a representation of small chunks of text (or binary data). 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 - + If I understand the question correctly, three string methods are Split, Scan, and Squeeze. 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. - + The two most common ways of defining a string literal are with single and double quotes. Text wrapped in single quotes + supports only the most basic escape sequence, the backslash. Double quoted text supports a wide range of escape sequences, + most notably newline characters (carriage returns). \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 2f188f6..8e7819f 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -13,14 +13,16 @@ @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end it "should be able to count the charaters" do - @my_string.should have(@my_string.size).characters + @my_string.should have(66).characters end - it "should be able to split on the . charater" do - result = @my_string.split('.') + it "should be able to split on the . character" do + result = @my_string.split(".") #do something with @my_string here result.should have(2).items end it "should be able to give the encoding of the string" do + puts "HERE: #{@my_string.encoding}" @my_string.encoding.should eq (Encoding.find("UTF-8")) + #should eq (Encoding.find("UTF-8")) end end end diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 50bc054..caf8ee0 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,4 +1,16 @@ class Book +<<<<<<< HEAD + attr_accessor :title, :page_count + + def initialize(title, page_count) + @title = title + @page_count = page_count + end + + def page_count + "Page count is #{@page_count}" + end +======= attr_accessor :title, :pages @@ -10,4 +22,5 @@ def initialize(title, pages) def page_count "Page count is #{@pages}" end +>>>>>>> 476e4b543ee68aad8bb809afdfe2207afd39e8e5 end diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 3af5583..3ab65f0 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -4,7 +4,18 @@ adjective = gets.chomp puts "Please enter a past tense action verb" verb_past_tense = gets.chomp +<<<<<<< HEAD +puts "Please enter a second noun" +noun2 = gets.chomp +puts "Please enter another adjective" +adjective2 = gets.chomp +puts "Please enter an exclamation" +exclamation = gets.chomp.upcase + +story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard. The #{noun} spotted a #{adjective2} #{noun2}, and yelled '#{exclamation}!'" +======= puts "What does the #{noun} say?" says = gets.chomp story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" +>>>>>>> 476e4b543ee68aad8bb809afdfe2207afd39e8e5 puts story diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..ecdcd28 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,16 +3,35 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? -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. - + They are very similar, in that they are both collections of objects, but an array can only be indexed + by an integer, while a hash can be indexed by any object (similar to an associative array in other languages, + but even more flexible - the key is not limited to just strings). + 2. When would you use an Array over a Hash and vice versa? -When the items have an inherent order I would use an array, when I want to reference the items in my collection by a name or key and their order does not matter I would use a hash. - + An array is very useful for building queues, as it logically lends itself well to working with ordering + and numbered positions within a collection. There is an obvious relationship between the index and + the logic of the situation. However, the flexibility of indexing in a has allows you to be more descriptive + or complex in your implementation of the collection - you can use a simple string to name values, or + go more complex and use an object as the key (although this might require implementing your own methods + for testing equality.) + 3. What is a module? Enumerable is a built in Ruby module, what is it? -A module is a way to group code that you can use across multiple classes. Enumerable is a Ruby module that provides collection functionality; iteration, searching, and sorting. It requires an implementation of the each method. - + A module is a collection of functionality under a common namespace. It is similar in concept to defining + some methods within a file, and including that file within another script, but with the added benefit + of preventing name collisions between different methods, which could easily occur with the file + inclusion method. + + Enumerable is, as stated, a module. It is used as a mixin for any collection class to provide it with + traversal and sorting functionality, given the host class provides implementations of a couple of basic sorting + and comparison methods. (e.g., the <=> method) + 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 can include multiple modules if you wanted to mix-in different functionality into your code. Code that is related with a hierarchical nature should be subclassed (inherited). A class can only have 1 direct parent, but can have lots of ancestors. + Ruby does not support multiple inheritance - however, if you require centralized functionality to be shared + between different classes, you can define it inside a module, and include that module within the different + classes (which is called a mixin). 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. + A class can be instantiated, while a module cannot. They are both good ways of collecting reusable + logic, but a class is best suited to representing some sort of logical object (serving as a blueprint), + whereas a module can be seen as a sort of library - simply a collection of functionality that can be included + where it is needed (and provide the benefits of multiple inheritance, without the downsides!) \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index fa35ccd..889e550 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,4 +1,25 @@ module SimonSays +<<<<<<< HEAD + def echo(input) + input + end + + def shout(input) + input.upcase + end + + def repeat(input, num_of_repeats = 2) + ((input + " ") * num_of_repeats).strip + end + + def start_of_word(input, num_of_characters = 1) + input[0...num_of_characters] + end + + def first_word(input) + input.split[0] + end +======= def echo(st) st end @@ -19,4 +40,6 @@ def repeat(st, t=2) return "Go Away!" if t==0 ([st]*t).join(' ') end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 end + diff --git a/week3/exercises/monster.rb b/week3/exercises/monster.rb index 013c3d2..ca0fe8c 100644 --- a/week3/exercises/monster.rb +++ b/week3/exercises/monster.rb @@ -2,7 +2,7 @@ class Monster include NamedThing attr_accessor :vulnerabilities, :dangers - attr_reader :nocturnal, :legs + attr_reader :nocturnal, :legs def initialize(noc, legs, name="Monster", vul = [], dangers = []) super(name) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index f8916e3..71636ba 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,4 +1,31 @@ class Calculator +<<<<<<< HEAD + + def sum(input) + output = 0 + input.each { |element| output += element } + output + end + + def multiply(input) + output = 1 #since multiplying by zero always equals zero + input.each { |element| output = output * element } + output + end + + def toThePowerOf(input, exponent) + input **= exponent + end + + def factorial(input) + output = 1 + for i in 1..input do + output *= i + end + output + end +end +======= def sum(array) array.inject(0){|sum, x| sum +x} end @@ -22,3 +49,4 @@ def pow_fac(base=nil, p) (1..p).to_a.inject(1){|f,v| f *= base || v} end end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..75d8000 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -27,6 +27,40 @@ # Once the above tests pass, # write tests and code for the following: describe "#multiply" do +<<<<<<< HEAD + it "multiplies two numbers" do + @calculator.multiply([2,3]).should == 6 + end + + it "multiplies an array of numbers" do + @calculator.multiply([2, 3, 5]).should == 30 + end + end + + describe "#toThePowerOf" do + it "raises one number to the power of another number" do + @calculator.toThePowerOf(2, 4).should == 16 + 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 +======= it "multiplies two numbers" do @calculator.multiply(2,2).should eq 4 end @@ -63,6 +97,7 @@ @calculator.fac(10).should eq 3628800 end +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 end end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index 08067b8..f942556 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,6 +5,27 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +<<<<<<< HEAD + A symbol is superficially similar to a string, but in reality is a constant and immutable + object. They are useful as names and identifiers, and commonly serve as hash keys. +2. What is the difference between a symbol and a string? + Strings can be modified, and multiple string objects can have the same contents and still be + different objects. Symbols, however, cannot be modified ( like a frozen string in this regard), and + the same symbol always refers to the same object. :test and :test have the same object ID, while + "test" and "test" would not. +3. What is a block and how do I call a block? + A block is an arbitrary chunk of Ruby code that can be supplied to an existing method call. It is called + by using a do-end following a method call(or curly brackets if the block is only one line) +4. How do I pass a block to a method? What is the method signature? + The method itself must be written to account for blocks - either by testing for the presence of a + supplied block and changing its flow based upon the result, or by being written to always expect + a block. At the point of method execution where the block should be started, it can be called + with the Yield signature and the appropriate number of arguments. +5. Where would you use regular expressions? + Regular expressions are useful for input parsing (e.g., ensuring a supplied email address is a valid + one). They can be used to strip undesirable characters from a string, or search a block of text + to find any instances of a search term. +======= A symbol is a static name or identifier. 2. What is the difference between a symbol and a string? @@ -15,6 +36,7 @@ A block is an anonymous function, or some code snipt that you can define and the 4. How do I pass a block to a method? What is the method signature? To pass a block to a method you define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. An example of passing a block to the each method of an array: +>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597 my_array.each {|a| puts a} diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index ce332af..4d281cd 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -1,5 +1,15 @@ -task :test do - puts "Hello World!" +config = {:basedirectory => "class"} + +task :makeclassdirectory do + Dir.mkdir(config[:basedirectory]) unless File.exists?(config[:basedirectory]) +end + +task :classmaker => ["makeclassdirectory"] do + File.open("names", "r") do |file| + file.each do |line| + Dir.mkdir(config[:basedirectory].to_s + "/" + line.chomp!) unless File.exists?(config[:basedirectory].to_s + "/" + line.chomp!) + end + end end task :make_class_dir do diff --git a/week5/exercises/classmaker.rb b/week5/exercises/classmaker.rb new file mode 100644 index 0000000..da5dbf6 --- /dev/null +++ b/week5/exercises/classmaker.rb @@ -0,0 +1,5 @@ +File.open("names", "r") do |file| + file.each do |line| + Dir.mkdir(line.chomp!) + end +end diff --git a/week6/class_materials/cbarcroft_gem b/week6/class_materials/cbarcroft_gem new file mode 160000 index 0000000..e1d8bea --- /dev/null +++ b/week6/class_materials/cbarcroft_gem @@ -0,0 +1 @@ +Subproject commit e1d8bea7ed1f22e2666fd6e05ebb4ee0e6462c82 diff --git a/week6/class_materials/gem/bin/test_gem b/week6/class_materials/gem/bin/test_gem new file mode 100644 index 0000000..e69de29 diff --git a/week6/class_materials/gem/lib/test_gem.rb b/week6/class_materials/gem/lib/test_gem.rb new file mode 100644 index 0000000..ceb5a6c --- /dev/null +++ b/week6/class_materials/gem/lib/test_gem.rb @@ -0,0 +1 @@ +puts "Hello" \ No newline at end of file diff --git a/week6/class_materials/gem/test_gem.gemspec b/week6/class_materials/gem/test_gem.gemspec new file mode 100644 index 0000000..7e254d5 --- /dev/null +++ b/week6/class_materials/gem/test_gem.gemspec @@ -0,0 +1,12 @@ +Gem::Specification.new do |s| + s.name = 'test_gem' + s.version = '0.0.0' + s.date = '2012-11-13' + s.summary = 'Making a Test Gem' + s.description = "A gem to explain how to make gems" + s.authors = ["Renee D, Chris"] + s.email = "chrisabarcroft@gmail.com" + s.homepage = '' + s.files = ['lib/test_gem.rb'] + s.executables << "test_gem" +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb index d2a6f6a..d5a148c 100644 --- a/week7/homework/features/step_definitions/pirate.rb +++ b/week7/homework/features/step_definitions/pirate.rb @@ -1,17 +1,15 @@ -class PirateTranslator - PIRATE_WORDS = { - "Hello Friend" => "Ahoy Matey" - } - def say(str) - @said = lookup_pirate(str).to_s +class Pirate + attr_accessor :phrase + + def initialize() + @phrase = "" end - + def translate - @said + "\n Shiber Me Timbers You Scurvey Dogs!!" + "Ahoy matey" end - -private - def lookup_pirate(str) - PIRATE_WORDS[str] + + def insult + "Shiber Me Timbers You Scurvey Dogs!!" end end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..b7c703b 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,19 +1,19 @@ -Gangway /^I have a (\w+)$/ do |arg| - @translator = Kernel.const_get(arg).new +Gangway /^I have a PirateTranslator$/ do + @pirate = Pirate.new end -Blimey /^I (\w+) '(.+)'$/ do |method, arg| - @translator.send(method, arg) +Blimey /^I say (.*)$/ do |arg1| + @pirate.phrase = arg1 end -Letgoandhaul /^I hit (\w+)$/ do |arg| - @result = @translator.send(arg) +Blimey /^I hit translate$/ do + @pirate.should_receive(:gets) end -Letgoandhaul /^it prints out '(.+)'$/ do |arg| - @result.split("\n ").first.should == arg +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @pirate.translate end -Letgoandhaul /^it also prints '(.+)'$/ do |arg| - @result.split("\n ").last.should == arg +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @pirate.insult end diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index b353120..0a2f15d 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,4 +1,5 @@ require 'rspec/mocks/standalone' +require './features/step_definitions/tic-tac-toe.rb' Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new @@ -35,7 +36,7 @@ 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| 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..990661d --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,127 @@ +class TicTacToe + attr_reader :player_symbol, :computer_symbol, :open_spots, :player_move, :computer_won, :player_won, :over + attr_accessor :board, :current_state + + SYMBOLS = [:X, :O] + WIN_SCENARIOS = { + 1 => ['A1','A2','A3'], + 2 => ['B1','B2','B3'], + 3 => ['C1','C2','C3'], + 4 => ['A1','B1','C1'], + 5 => ['A2','B2','C2'], + 6 => ['A3','B3','C3'], + 7 => ['A1','B2','C3'], + 8 => ['C1','B2','A3'] + } + + def initialize( first_turn = false, chosen_symbol = false) + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + @players = {:player => "Player", :computer => "Computer"} + @current_player = first_turn + if first_turn == false + @current_player = [:computer, :player].sample + end + if chosen_symbol == false + @player_symbol = :X + @computer_symbol = :O + else + @player_symbol = chosen_symbol + @computer_symbol = SYMBOLS.select{|x| x != chosen_symbol}[0] + end + @computer_won = false + @player_won = false + @over = false + end + + def current_player + @players[@current_player] + end + + def player=(str) + @players[:player] = str + end + + def player + @players[:player] + end + + def open_spots + @board.select{|x,y| y == " "}.keys + end + + def current_state + str = "#{board[:A1]} | #{board[:A2]} | #{board[:A3]} \n" + str += "#{board[:B1]} | #{board[:B2]} | #{board[:B3]} \n" + str += "#{board[:C1]} | #{board[:C2]} | #{board[:C3]} \n" + str + end + + def welcome_player + "Welcome #{@players[:player]}" + end + + def indicate_player_turn + puts "#{@players[@current_player]}'s Move:" + end + + def get_player_move + gets.chomp + end + + def player_move + move = self.get_player_move.to_sym + if !self.open_spots.include? move + move = self.get_player_move.to_sym + end + @board[move] = @player_symbol + @current_player = :computer + move + end + + def computer_move + puts "Computer's Turn:" + sleep(1) + move = self.open_spots.sample + @board[move] = @computer_symbol + @current_player = :player + move + end + + def spots_open? + !self.open_spots.empty? + end + + def draw? + (!self.spots_open? && !@player_won && !computer_won) ? true : false + end + + def over? + (self.draw? || @player_won || @computer_won)? true : false + end + + def player_won? + @player_won? true : false + end + + def computer_won? + @computer_won? true : false + end + + def determine_winner + WIN_SCENARIOS.each do |key, scenario| + if (!@player_won || @computer_won) + @player_won = true + @computer_won = true + scenario.each do | space | + @player_won = false if @board[space.to_sym] != @player_symbol + @computer_won = false if @board[space.to_sym] != @computer_symbol + end + end + end + @over = self.over? + end +end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -8,7 +8,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 diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..d72b456 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,30 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + Method_missing is a sort of catch-all that is triggered when you attempt to use an undefined method + from a particular object. You can use this to create a sort of dynamic method definition, such + as Rails' find_by_ methods - they will parse the provided method name and use the end of it (e.g, + find_by_name would yield 'name') as the database column to search on. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + An eigenclass is a special class created specifically for one instance of a class (not the entire class) + These are created to implement Singleton methods, which are available only to that one instance of the + greater class. When a singleton method is defined, Ruby automatically creates a new class(this is the Eigenclass) + and assigns the object as a child class of the new one, which contains the singleton methods. + 3. When would you use DuckTypeing? How would you use it to improve your code? + Ducktyping makes code more intuitive and readable by treating objects by the methods they implement, + rather than as a predefined type. A good example of this is using the enumerable mixin to make custom + data types respond to set operations - in that case, we don't care what type of data we are dealing with, + we just care that it responds successfully to 'sum' or 'succ', or any other set operation. + 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 called on a class, but instance methods are called only on objects instantiated + from the class. This means that class methods can be called without instantiating the class. + instance_eval can be used to dynamically add a class method to a class, while class_eval can be used + to dynamically add an instance method to an object. + 5. What is the difference between a singleton class and a singleton method? + A singleton class is the 'anonymous' class that is created to hold methods that are assigned to + a single instance of a class. Singleton methods are the individual methods that are assigned to + the single class instances. diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..533d300 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -1,4 +1,14 @@ class Couch +<<<<<<< HEAD + def initialize(pillow_colors, cushion_colors) + @pillow_colors = pillow_colors + @cushion_colors = cushion_colors + end + + [:pillow_colors, :cushion_colors].each do |s| + define_method("list_#{s}") do + instance_variable_get("@#{s}").join(", ") +======= def initialize(pillows, cushions) @pillows = pillows @cushions = cushions @@ -15,6 +25,7 @@ def cushion_colors [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count +>>>>>>> 4b2958513d06a8a3cac07f3785ec431b77644ba7 end end end