From b494aaf53dd292e00fa647ac81c36e8b8997381b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20De=20Voursney?= Date: Thu, 9 Jan 2014 21:09:06 -0800 Subject: [PATCH 01/16] class work from week 1 --- week1/exercises/rspec_spec.rb | 14 +++++++++----- week1/ruby_spec.rb | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 week1/ruby_spec.rb diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index b00c711..cbdfb25 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -2,7 +2,7 @@ describe "The Rspec ruby gem" do - context "Domain Specific Language" do + context "Domain Specific Language" do it "creates examples with the #it keyword" do @@ -77,15 +77,19 @@ # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - (1+2-5*6/2).should eq -6 + ((((1+2)-5)*6)/2).should eq -6 end it "should count the characters in your name" do - pending + "Tom".should have(3).characters end - it "should check basic math" + it "should check basic math" do + (40+2).should eq 42 + end - it "should check basic spelling" + it "should check basic spelling" do + "Field".should include('ie') + end end diff --git a/week1/ruby_spec.rb b/week1/ruby_spec.rb new file mode 100644 index 0000000..ad6e99a --- /dev/null +++ b/week1/ruby_spec.rb @@ -0,0 +1,11 @@ +describe "Playing with Ruby!" do + + context 'when adding numbers' do + + it "should add numbers" do + (2+2).should eq 4 + end + + end + +end \ No newline at end of file From 9695a1383d8777d5f31c1bfb05e69bcc3f466184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20De=20Voursney?= Date: Thu, 16 Jan 2014 19:01:09 -0800 Subject: [PATCH 02/16] the rspec answers from week1 --- week1/homework/strings_and_rspec_spec.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..0aca0c0 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,21 @@ 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 split on the . charater" do - pending - result = #do something with @my_string here + + it "allows us to test for empty strings" do + @my_string.should_not be_empty + end + + it "should be able to count the characters" do + @my_string.should have(@my_string.length).characters + end + + it "should be able to split on the . character" do + 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.should eq (Encoding.find("UTF-8")) end end end From 79563e6e5de30eab9f150dd6ff9d60cedc85f0a3 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 22 Jan 2014 20:37:29 -0800 Subject: [PATCH 03/16] Home work for week1 and week2. --- week1/homework/.rspec | 2 ++ week2/homework/.rspec | 2 ++ week2/homework/questions.txt | 14 ++++++++++++ week2/homework/simon_says.rb | 41 ++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 week1/homework/.rspec create mode 100644 week2/homework/.rspec create mode 100644 week2/homework/simon_says.rb diff --git a/week1/homework/.rspec b/week1/homework/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week1/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week2/homework/.rspec b/week2/homework/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week2/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..ac0389e 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,24 @@ 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. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..f7b1654 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,41 @@ + +module SimonSays + +def echo(str) + str +end + +def shout(str) + str.upcase +end + +def doRepeat(str) + str.concat(" ".concat(str)) +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 \ No newline at end of file From 1913bc13d7da76dd9b428375f4829f3a8d3f21db Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 22 Jan 2014 20:54:18 -0800 Subject: [PATCH 04/16] Adding missed week1 homework files --- week1/homework/questions.txt | 20 ++++++++++++++++++++ week1/homework/strings_and_rspec_spec.rb | 21 ++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..8c75d6f 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,33 @@ 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. \ 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 ea79e4c..a4be9a1 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -10,16 +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 From 5d74d1c0591bcc3a0081f955f1b419029ec8b3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20De=20Voursney?= Date: Thu, 23 Jan 2014 17:46:13 -0800 Subject: [PATCH 05/16] week1 answers --- week1/homework/questions.txt | 10 +++++++++- week1/homework/strings_and_rspec_spec.rb | 11 ++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..f7276ac 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,21 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. 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. 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). 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[]). 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 -6. What are two ways of defining a String literal? Bonus: What is the difference between them? +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 quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same. \ 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 0aca0c0..14cbcdc 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,16 +12,10 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - - it "allows us to test for empty strings" do - @my_string.should_not be_empty - end - it "should be able to count the characters" do - @my_string.should have(@my_string.length).characters + @my_string.should have(@my_string.size).characters end - - it "should be able to split on the . character" do + it "should be able to split on the . charater" do result = @my_string.split('.') result.should have(2).items end @@ -30,4 +24,3 @@ end end end - From 3f908987d4313d0076dc2a2b4164ffb931a8aa80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20De=20Voursney?= Date: Thu, 23 Jan 2014 17:47:54 -0800 Subject: [PATCH 06/16] week 2 hw answers --- week2/homework/questions.txt | 5 +++++ week2/homework/simon_says.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..4cfc0a3 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,16 @@ 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. 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. 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. 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. 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. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..971a674 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + 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 From cba25955989e180ca8e193c9e3162b72b1fb0329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20De=20Voursney?= Date: Thu, 23 Jan 2014 21:07:32 -0800 Subject: [PATCH 07/16] week3 in-class work --- week2/homework/.rspec | 2 ++ week2/homework/simon_says.rb | 38 ++++++++++++++++++---------------- week3/exercises/book.rb | 30 +++++++++++++++++++++++---- week3/exercises/book_spec.rb | 31 +++++++++++++++++++++++---- week3/exercises/human.rb | 6 ++++++ week3/exercises/monster.rb | 6 ++++++ week3/exercises/other_thing.rb | 5 +++++ week3/exercises/vampire.rb | 10 ++++++++- week3/exercises/zombie.rb | 8 +++++++ 9 files changed, 109 insertions(+), 27 deletions(-) create mode 100644 week2/homework/.rspec create mode 100644 week3/exercises/human.rb create mode 100644 week3/exercises/other_thing.rb create mode 100644 week3/exercises/zombie.rb diff --git a/week2/homework/.rspec b/week2/homework/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week2/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 971a674..356867b 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,21 +1,23 @@ module SimonSays - def echo(st) - st - end - - def shout(st) - st.upcase - end - def first_word(st) - st.split.first - end + def echo input + input + end - def start_of_word(st,i) - st[0...i] - end - - def repeat(st, t=2) - ([st]*t).join(' ') - end -end + def shout input + input.upcase + end + + def repeat input, n = 2 + ([input] * n ).join(' ') + end + + def start_of_word input, n + input.slice(0,n) + end + + def first_word sentance + sentance.split.first + end + +end \ No newline at end of file diff --git a/week3/exercises/book.rb b/week3/exercises/book.rb index c13e4d4..7f89250 100644 --- a/week3/exercises/book.rb +++ b/week3/exercises/book.rb @@ -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 \ No newline at end of file diff --git a/week3/exercises/book_spec.rb b/week3/exercises/book_spec.rb index 72bc203..a747ba4 100644 --- a/week3/exercises/book_spec.rb +++ b/week3/exercises/book_spec.rb @@ -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 \ No newline at end of file diff --git a/week3/exercises/human.rb b/week3/exercises/human.rb new file mode 100644 index 0000000..51c2855 --- /dev/null +++ b/week3/exercises/human.rb @@ -0,0 +1,6 @@ +require_relative 'named_thing' +require_relative 'other_thing' +class Human + include NamedThing + include OtherThing +end \ No newline at end of file diff --git a/week3/exercises/monster.rb b/week3/exercises/monster.rb index 013c3d2..9293138 100644 --- a/week3/exercises/monster.rb +++ b/week3/exercises/monster.rb @@ -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 diff --git a/week3/exercises/other_thing.rb b/week3/exercises/other_thing.rb new file mode 100644 index 0000000..f51fdd2 --- /dev/null +++ b/week3/exercises/other_thing.rb @@ -0,0 +1,5 @@ +module OtherThing + def say_name + "hello" + end +end \ No newline at end of file diff --git a/week3/exercises/vampire.rb b/week3/exercises/vampire.rb index 764adf6..c729ede 100644 --- a/week3/exercises/vampire.rb +++ b/week3/exercises/vampire.rb @@ -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 diff --git a/week3/exercises/zombie.rb b/week3/exercises/zombie.rb new file mode 100644 index 0000000..692980e --- /dev/null +++ b/week3/exercises/zombie.rb @@ -0,0 +1,8 @@ +require_relative 'named_thing' +class Zombie + include NamedThing + + def say_name + "uuurrrggghhhh #{@name}" + end +end \ No newline at end of file From 68c394306b6809e0d29a10d61d4baf4a9317cdf0 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 29 Jan 2014 20:43:39 -0800 Subject: [PATCH 08/16] Resolve conflicts of week1 & week2 homework due to merging answers branch onto master --- week1/homework/questions.txt | 20 +--------------- week1/homework/strings_and_rspec_spec.rb | 12 ---------- week2/homework/questions.txt | 9 +------ week2/homework/simon_says.rb | 30 ------------------------ 4 files changed, 2 insertions(+), 69 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index b554435..0c60765 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,7 +3,6 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? -<<<<<<< HEAD Object is an instance of a class having: a. Unique internal state b. Attributes that expose internal state to external world @@ -15,27 +14,13 @@ 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. -======= -An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. - -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. - -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). ->>>>>>> cba25955989e180ca8e193c9e3162b72b1fb0329 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[]). 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 -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 -<<<<<<< HEAD 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 @@ -48,7 +33,4 @@ a. Using single-quote .i.e singleQuotedStr = 'This is a single quoted string'. T 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. -======= -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 quotes allow for 2 escape characters: \' and \\ . The double quoted 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 quoted string takes up much less memory than a double quoted string with interpolation. Without interpolation, both are about the same. ->>>>>>> cba25955989e180ca8e193c9e3162b72b1fb0329 + diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 3f2a28f..7b79d1f 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,7 +12,6 @@ before(:all) do @myString = "JayaPrakash likes programming and mystery novels" end -<<<<<<< HEAD it "should be able to count the charaters" do (@myString.length).should eq 48 end @@ -28,17 +27,6 @@ (@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 -======= - it "should be able to count the characters" do - @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 - end - it "should be able to give the encoding of the string" do - @my_string.encoding.should eq (Encoding.find("UTF-8")) ->>>>>>> cba25955989e180ca8e193c9e3162b72b1fb0329 end end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 7e02d6a..79ca140 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,17 +3,14 @@ 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. 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? -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. 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? -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. Module defines a namespace, grouping together methods, constants and classes, which can be mixed-in into other classes. @@ -21,15 +18,11 @@ 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? -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. 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? -<<<<<<< HEAD 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. -======= -A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. ->>>>>>> cba25955989e180ca8e193c9e3162b72b1fb0329 + diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 317a21b..6184a9b 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,5 +1,3 @@ -<<<<<<< HEAD - module SimonSays def echo(str) @@ -10,10 +8,6 @@ def shout(str) str.upcase end -def doRepeat(str) - str.concat(" ".concat(str)) -end - def repeat(*args) str = nil count = 2 @@ -38,28 +32,4 @@ def start_of_word(str, count) def first_word(str) str.split[0] end -======= -module SimonSays - - def echo input - input - end - - def shout input - input.upcase - end - - def repeat input, n = 2 - ([input] * n ).join(' ') - end - - def start_of_word input, n - input.slice(0,n) - end - - def first_word sentance - sentance.split.first - end ->>>>>>> cba25955989e180ca8e193c9e3162b72b1fb0329 - end \ No newline at end of file From d479e14c00863df003c87fc09dc1d83470268d85 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 29 Jan 2014 20:46:39 -0800 Subject: [PATCH 09/16] week3 homework submission --- week3/homework/.rspec | 2 ++ week3/homework/calculator.rb | 20 ++++++++++++++++++++ week3/homework/calculator_spec.rb | 12 ++++++++++++ week3/homework/questions.txt | 29 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 week3/homework/.rspec create mode 100644 week3/homework/calculator.rb diff --git a/week3/homework/.rspec b/week3/homework/.rspec new file mode 100644 index 0000000..0eb654b --- /dev/null +++ b/week3/homework/.rspec @@ -0,0 +1,2 @@ +--color +--format nested diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..6027058 --- /dev/null +++ b/week3/homework/calculator.rb @@ -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 \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..fe8f057 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -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, @@ -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 diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..410b9d0 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -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 \ No newline at end of file From fb20c4d2b9b78655ad4293222e517fb41e584f02 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 5 Feb 2014 18:45:33 -0800 Subject: [PATCH 10/16] Week4 home work --- week4/homework/.rspec | 3 +++ week4/homework/questions.txt | 15 +++++++++++++++ week4/homework/worker.rb | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 week4/homework/.rspec create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/.rspec b/week4/homework/.rspec new file mode 100644 index 0000000..c47710a --- /dev/null +++ b/week4/homework/.rspec @@ -0,0 +1,3 @@ +--color +--color +--format nested diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..708fbb8 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,11 +4,26 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby reads files using File.open method. + +File.open(filename, mode) where mode can be r/w/r+ to get read-only/write-only/read-write access to the file. + 2. How would you output "Hello World!" to a file called my_output.txt? +f = File.open("my_output.txt", w) +f.puts("Hello World!") + 3. What is the Directory class and what is it used for? +Dir class is used to represnt directory in the underlying file system. It provides various methods to work with directories. + 4. What is an IO object? +IO object is the base class that handles input into and output from a ruby program. + 5. What is rake and what is it used for? What is a rake task? +Rake is the build tool used to define build tasks for ruby applications similar to ant/make for java/cpp applications. A rake task is used to define a build task. Its a method that takes two parameters: a map having dependencies and a code block that need to be passed to task. + + + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..91ab211 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,7 @@ +class Worker + def self.work(n=1) + a = 0 + n.times {a = yield} if block_given? + a + end +end \ No newline at end of file From 8d929da2fad5531ab5be4b746074c0dcf7947c5d Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Sat, 22 Feb 2014 06:12:19 -0800 Subject: [PATCH 11/16] week7 homework: pirate translator --- .../features/step_definitions/library.rb | 12 ++++++++++++ .../step_definitions/pirate_translator.rb | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 week7/homework/features/step_definitions/library.rb create mode 100644 week7/homework/features/step_definitions/pirate_translator.rb diff --git a/week7/homework/features/step_definitions/library.rb b/week7/homework/features/step_definitions/library.rb new file mode 100644 index 0000000..b534194 --- /dev/null +++ b/week7/homework/features/step_definitions/library.rb @@ -0,0 +1,12 @@ +module Library + + DICTIONARIES = { + pirate: { + "Hello Friend" => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + } + } + + def getDictionary type + DICTIONARIES[type] + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_translator.rb b/week7/homework/features/step_definitions/pirate_translator.rb new file mode 100644 index 0000000..6ef8cc3 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_translator.rb @@ -0,0 +1,16 @@ +require "#{File.dirname(__FILE__)}//library" + +class PirateTranslator + include Library + def initialize + @dictionary = getDictionary(:pirate) + end + + def say key + @result = @dictionary[key] + end + + def translate + @result + end +end \ No newline at end of file From 704c18a434942a8c8b4498bf30722ab602704a07 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Tue, 25 Feb 2014 22:46:01 -0800 Subject: [PATCH 12/16] Answers for week7 questions --- week7/homework/questions.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..78405ff 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 method, which is invoked by Ruby when a message is sent to an object that can't handle it. + + It can be used to simulate the existence of methods or gracefully handle a situation when object receives a message that it can't handle. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + + Eigenclass also called as Singleton class, is an anonymous class defined by Ruby when class/singleton methods are defined. + + ALl the class/singleton methods live in 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? + + Class method is a method that can be invoked without having an instance of a class, where as instance method needs an instance object of a class to be invoked. + + instance_eval is executed in the context of singleton class on which it is invoked, where as class_eval is executed in the context of class definition. So any methods defined inside instance_eval will become class methods and any methods defined inside class_eval will become instance methods. + 5. What is the difference between a singleton class and a singleton method? + + A singleton method is a method that exists only for an instance of an object. + + A singleton class contains singleton methods defined for an instance of object. From 96ba0736fea7d29a9b60f1cd26695dd60b6fb8a8 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Wed, 26 Feb 2014 20:38:54 -0800 Subject: [PATCH 13/16] Move pirate talk related work to different folder. Updated answers to questions. --- week7/homework/{ => piratetalk}/features/pirate.feature | 0 .../{ => piratetalk}/features/step_definitions/library.rb | 0 .../{ => piratetalk}/features/step_definitions/pirate_steps.rb | 0 .../features/step_definitions/pirate_translator.rb | 0 week7/homework/questions.txt | 2 ++ 5 files changed, 2 insertions(+) rename week7/homework/{ => piratetalk}/features/pirate.feature (100%) rename week7/homework/{ => piratetalk}/features/step_definitions/library.rb (100%) rename week7/homework/{ => piratetalk}/features/step_definitions/pirate_steps.rb (100%) rename week7/homework/{ => piratetalk}/features/step_definitions/pirate_translator.rb (100%) diff --git a/week7/homework/features/pirate.feature b/week7/homework/piratetalk/features/pirate.feature similarity index 100% rename from week7/homework/features/pirate.feature rename to week7/homework/piratetalk/features/pirate.feature diff --git a/week7/homework/features/step_definitions/library.rb b/week7/homework/piratetalk/features/step_definitions/library.rb similarity index 100% rename from week7/homework/features/step_definitions/library.rb rename to week7/homework/piratetalk/features/step_definitions/library.rb diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/piratetalk/features/step_definitions/pirate_steps.rb similarity index 100% rename from week7/homework/features/step_definitions/pirate_steps.rb rename to week7/homework/piratetalk/features/step_definitions/pirate_steps.rb diff --git a/week7/homework/features/step_definitions/pirate_translator.rb b/week7/homework/piratetalk/features/step_definitions/pirate_translator.rb similarity index 100% rename from week7/homework/features/step_definitions/pirate_translator.rb rename to week7/homework/piratetalk/features/step_definitions/pirate_translator.rb diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index 78405ff..170670a 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -17,7 +17,9 @@ Questions: 3. When would you use DuckTypeing? How would you use it to improve your code? + If I understand correctly, I use duck typing everywhere as I don't specify the type of parameters or check for the type of variables passed into methods. + I would use it to make my code concise and readable. As the type of objects that can be handled is determined based upon what they can do, code I create would be generic. 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? From ef0cfd59b783306df09bf8d9d7c5a957a3cb63d9 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Thu, 27 Feb 2014 18:08:16 -0800 Subject: [PATCH 14/16] Moving tictactoe to different folder --- .../step_definitions/tic-tac-toe-steps.rb | 0 .../features/step_definitions/tic_tac_toe.rb | 23 +++++++++++++++++++ .../features/tic-tac-toe.feature | 0 week7/homework/{ => tictactoe}/play_game.rb | 0 4 files changed, 23 insertions(+) rename week7/homework/{ => tictactoe}/features/step_definitions/tic-tac-toe-steps.rb (100%) create mode 100644 week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb rename week7/homework/{ => tictactoe}/features/tic-tac-toe.feature (100%) rename week7/homework/{ => tictactoe}/play_game.rb (100%) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb similarity index 100% rename from week7/homework/features/step_definitions/tic-tac-toe-steps.rb rename to week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb diff --git a/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb b/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb new file mode 100644 index 0000000..ca48b47 --- /dev/null +++ b/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb @@ -0,0 +1,23 @@ +class TicTacToe + + SYMBOLS = ['O', 'X'] + + attr_accessor :player, :player_symbol, :computer_symbol + + def initialize first_player=:computer, second_player= + + + @player = name + @turn = rand(0..1) + @player_symbol = SYMBOLS[@turn] + @computer_symbol = SYMBOLS[1 ^ @turn] + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + (@turn.eql? 1) ? @player : "Computer" + end +end diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/tictactoe/features/tic-tac-toe.feature similarity index 100% rename from week7/homework/features/tic-tac-toe.feature rename to week7/homework/tictactoe/features/tic-tac-toe.feature diff --git a/week7/homework/play_game.rb b/week7/homework/tictactoe/play_game.rb similarity index 100% rename from week7/homework/play_game.rb rename to week7/homework/tictactoe/play_game.rb From 67cfee4cf9d37fa01d22c184f40010d95d8207c1 Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Sat, 8 Mar 2014 04:45:57 -0800 Subject: [PATCH 15/16] First working version of TicTacToe! --- .../features/step_definitions/converter_jp.rb | 21 ++++ .../step_definitions/converter_steps_jp.rb | 15 +++ .../step_definitions/tic-tac-toe-steps.rb | 2 +- .../features/step_definitions/tic-tac-toe.rb | 117 ++++++++++++++++++ .../features/step_definitions/tic_tac_toe.rb | 23 ---- week7/homework/tictactoe/play_game.rb | 6 +- 6 files changed, 158 insertions(+), 26 deletions(-) create mode 100644 week7/exercises/features/step_definitions/converter_jp.rb create mode 100644 week7/exercises/features/step_definitions/converter_steps_jp.rb create mode 100644 week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb delete mode 100644 week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb mode change 100644 => 100755 week7/homework/tictactoe/play_game.rb diff --git a/week7/exercises/features/step_definitions/converter_jp.rb b/week7/exercises/features/step_definitions/converter_jp.rb new file mode 100644 index 0000000..eb39b76 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter_jp.rb @@ -0,0 +1,21 @@ +class Converter + + attr_accessor :temp, :type, :degrees_f, :degrees_c + + def initialize temp + @temp = temp + end + + def convert + send "#{@type}"_converter + end + +private + def Fahrenheit_converter + @degrees_c = (@temp - 32) * (5/9) + end + + def Celsius_converter + @degrees_f = (@temp * (9/5)) + 32 + end +end \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/converter_steps_jp.rb b/week7/exercises/features/step_definitions/converter_steps_jp.rb new file mode 100644 index 0000000..12ea024 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter_steps_jp.rb @@ -0,0 +1,15 @@ +Given(/^I have entered (\d+) into the converter$/) do |temp| + @converter = Converter.new(temp) +end + +Given(/^I set the type to (".*"?)$/) do |type| + @converter.type = type +end + +When(/^I press convert$/) do + @converter.convert +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + @converter.degrees_c = "#{arg1}.#{arg2}" +end \ No newline at end of file diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..a9bd88e 100644 --- a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,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/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..7b5ab7a --- /dev/null +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,117 @@ +#require File.expand_path(File.dirname(__FILE__)) + '/tic_tac_toe_@board.rb' + +class TicTacToe + + SYMBOLS = [:O, :X] + + attr_accessor :player, :player_symbol, :computer_symbol, :board + + def initialize player1=nil, player2=nil + init_game(player1, player2) + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + (@current_player.eql? :player) ? @player : "Computer" + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def draw? + (spots_open?.eql? false) && (determine_winner.eql? false) + end + + def over? + (draw? || determine_winner) + end + + def spots_open? + open_spots.length > 0 + end + + def open_spots + @spots.reject{|x| @board.key? x.intern} + end + + def current_state + "| #{@board[:A1].to_s} | #{@board[:A2].to_s} | #{@board[:A3].to_s} |\n" + + "| #{@board[:B1].to_s} | #{@board[:B2].to_s} | #{@board[:B3].to_s} |\n" + + "| #{@board[:C1].to_s} | #{@board[:C2].to_s} | #{@board[:C3].to_s} |\n" + end + + def determine_winner + (player_won? || computer_won?) + end + + [:player, :computer].each do |e| + define_method("#{e}_won?") do + if ((@board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A2] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B2] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C2] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:C1] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:C2] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:C3] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:A1] == @board[:B2] && @board[:B2] == @board[:C3] && @board[:C3] == instance_variable_get("@#{e.to_s}_symbol")) || + (@board[:A3] == @board[:B2] && @board[:B2] == @board[:C1] && @board[:C1] == instance_variable_get("@#{e.to_s}_symbol"))) + return true + else + return false + end + end + end + + def player_move + move = get_player_move + until is_valid_move(move) + puts "Not a valid move. Try Again:" + move = get_player_move + end + @board[move.intern] = @player_symbol + @current_player = :computer + move.intern + end + + def computer_move + move = open_spots[rand(0..(open_spots.length - 1))] + @board[move.intern] = @computer_symbol + @current_player = :player + move + end + + private + + def init_game(player1, player2) + + @current_player = player1 + toss = rand(0..1) + + if (player2 != nil) + if (@current_player.eql? :player) + @player_symbol = SYMBOLS.reject{|x| x.eql? player2}[0] + @computer_symbol = player2 + else ((@current_player.eql? :computer)) + @computer_symbol = SYMBOLS.reject{|x| x.eql? player2}[0] + @player_symbol = player2 + end + else + @current_player = ((toss.eql? 1) ? :player : :computer) if (@current_player.eql? nil) + @player_symbol = SYMBOLS[toss] + @computer_symbol = SYMBOLS[1 ^ toss] + end + @spots = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"] + @board = Hash.new(" ") + end + + def is_valid_move move + (@board[move.intern].eql? " ") && (@spots.include? move) + end +end diff --git a/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb b/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb deleted file mode 100644 index ca48b47..0000000 --- a/week7/homework/tictactoe/features/step_definitions/tic_tac_toe.rb +++ /dev/null @@ -1,23 +0,0 @@ -class TicTacToe - - SYMBOLS = ['O', 'X'] - - attr_accessor :player, :player_symbol, :computer_symbol - - def initialize first_player=:computer, second_player= - - - @player = name - @turn = rand(0..1) - @player_symbol = SYMBOLS[@turn] - @computer_symbol = SYMBOLS[1 ^ @turn] - end - - def welcome_player - "Welcome #{@player}" - end - - def current_player - (@turn.eql? 1) ? @player : "Computer" - end -end diff --git a/week7/homework/tictactoe/play_game.rb b/week7/homework/tictactoe/play_game.rb old mode 100644 new mode 100755 index 0535830..cf546f5 --- a/week7/homework/tictactoe/play_game.rb +++ b/week7/homework/tictactoe/play_game.rb @@ -1,3 +1,4 @@ +#!/usr/bin/env ruby require './features/step_definitions/tic-tac-toe.rb' @game = TicTacToe.new @@ -8,9 +9,10 @@ until @game.over? case @game.current_player when "Computer" - @game.computer_move + puts "Computer's Move:" + puts @game.computer_move when @game.player - @game.indicate_palyer_turn + @game.indicate_player_turn @game.player_move end puts @game.current_state From c4ee08e2f32a830de00cf897fe2303da77bb41da Mon Sep 17 00:00:00 2001 From: JayaPrakash Reddy Atla Date: Sat, 8 Mar 2014 04:49:49 -0800 Subject: [PATCH 16/16] Class room work of week 8 --- week8/exercises/.rspec | 2 ++ week8/exercises/couch.rb | 51 +++++++++++++++++++++++++++++------ week8/exercises/couch_spec.rb | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 week8/exercises/.rspec diff --git a/week8/exercises/.rspec b/week8/exercises/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week8/exercises/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index b32ea96..3d66793 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -11,16 +11,51 @@ def initialize pillows, cushions, dogs end end - def pillow_colors - @pillows.map &:to_s - end + def method_missing m, *args, &block + puts "1. #{self}" + puts "2. #{self.class}" + # The following thing creates stack overflow error + # self.instance_eval do + # define_method "#{m}_self_instance_eval" do + # puts "self.instance_eval #{m}" + # end + # end - def cushions_colors - @cushions.map &:to_s - end + # The following thing creates stack overflow error + # self.class_eval do + # define_method "#{m}_self_class_eval" do + # puts "self.instance_eval #{m}" + # end + # end - def dog_names - @dogs.map &:to_s + #Dont know where the method would be defined after this. + self.class.instance_eval do + define_method "#{m}_self_class_instance_eval" do + puts "self.class.instance_eval #{m}" + end + end + + self.class.class_eval do + define_method "#{m}_self_class_class_eval" do + puts "self.class.class_eval #{m}" + end + end end + # def method_missing m, *args, &block + # instance_variable_get("@#{m.to_s.split("_")[0]}s").map &:to_s + # end + + # def pillow_colors + # @pillows.map &:to_s + # end + + # def cushions_colors + # @cushions.map &:to_s + # end + + # def dog_names + # @dogs.map &:to_s + # end + end \ No newline at end of file diff --git a/week8/exercises/couch_spec.rb b/week8/exercises/couch_spec.rb index 8af3ee0..71b78ac 100644 --- a/week8/exercises/couch_spec.rb +++ b/week8/exercises/couch_spec.rb @@ -9,7 +9,7 @@ end it "should tell me the cushions colors" do - @couch.cushions_colors.should eq ["grey", "grey"] + @couch.cushion_colors.should eq ["grey", "grey"] end it "should tell me the dogs names" do