diff --git a/.gitignore b/.gitignore index e43b0f9..07fbbce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store +.*.swp +.*.un~ + diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt deleted file mode 100644 index e38c84d..0000000 --- a/midterm/instructions_and_questions.txt +++ /dev/null @@ -1,39 +0,0 @@ -Instructions for Mid-Term submission and Git Review (10pts): - - Create a git repository for your answers - - Add and Commit as you work through the questions and programming problems - - Your git log should reflect your work, don't just commit after you have finished working - - Use .gitignore to ignore any files that are not relevant to the midterm - - E-mail me your ssh public key - - I will email you back with your repository name - - Add a remote to your git repository: git@reneedv.com:RubyWinter2014/YOURREPOSITORYNAME.git - - Push your changes to the remote - - After 6pm Thursday February 20th you will not be able to push to your remote repository (or clone). - - Questions (20pts): - - What are the three uses of the curly brackets {} in Ruby? - - 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? - 1. x, y = "hello", "hello" - 2. x = y = "hello" -- What is the difference between a Range and an Array? -- Why would I use a Hash instead of an Array? -- What is your favorite thing about Ruby so far? -- What is your least favorite thing about Ruby so far? - - Programming Problems (10pts each): - - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. - - The EvenNumber class should: - - Only allow even numbers - - Get the next even number - - Compare even numbers - - Generate a range of even numbers -- Make the rspec tests in wish_list_spec.rb pass by writing a WishList class - - The WishList class should: - - Mixin Enumerable - - Define each so it returns wishes as strings with their index as part of the string - -Mid-Term Spec (50pts): -- Make the tests pass. - - diff --git a/midterm/mid_term_spec.rb b/midterm/mid_term_spec.rb deleted file mode 100644 index 0556a97..0000000 --- a/midterm/mid_term_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -require "#{File.dirname(__FILE__)}/turkey" - -describe Turkey do - - before do - @turkey = Turkey.new(10) - end - - it "should report the turkey weight" do - @turkey.weight.should equal 10 - end - - it "should be a kind of animal" do - @turkey.kind_of?(Animal).should be_true - end - - it "should gobble speak" do - @turkey.gobble_speak("Hello I Am a Turkey. Please Don't Eat Me.").should eq "Gobble Gobble Gobble gobble Gobble. Gobble Gobb'le Gobble Gobble." - end - -end - -require "#{File.dirname(__FILE__)}/thanksgiving_dinner" - -describe ThanksgivingDinner do - - before do - @t_dinner = ThanksgivingDinner.new(:vegan) - @t_dinner.guests = ["Aunt Petunia", "Uncle Vernon", "Aunt Marge", "Dudley", "Harry"] # I know I just made a British family celebrate Thanksgiving, but it could be worse: It could have been the 4th of July! :) - end - - it "should be a kind of dinner" do - @t_dinner.kind_of?(Dinner).should be_true - end - - # Use inject here - it "should sum the letters in each guest name for the seating chart size" do - @t_dinner.seating_chart_size.should eq 45 - end - - it "should provide a menu" do - @t_dinner.respond_to?(:menu).should be_true - end - - context "#menu" do - - it "should have a diet specified" do - @t_dinner.menu[:diet].should eq :vegan - end - - it "should have proteins" do - @t_dinner.menu[:proteins].should eq ["Tofurkey", "Hummus"] - end - - it "should have vegetables" do - @t_dinner.menu[:veggies].should eq [:ginger_carrots , :potatoes, :yams] - end - - # Dinners don't always have dessert, but ThanksgivingDinners always do! - it "should have desserts" do - @t_dinner.menu[:desserts].should eq({:pies => [:pumkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}) - end - - end - - # Use String interpolation, collection methods, and string methods for these two examples - it "should return what is on the dinner menu" do - @t_dinner.whats_for_dinner.should eq "Tonight we have proteins Tofurkey and Hummus, and veggies Ginger Carrots, Potatoes, and Yams." - end - - it "should return what is on the dessert menu" do - @t_dinner.whats_for_dessert.should eq "Tonight we have 5 delicious desserts: Pumkin Pie, Chocolate Moose, and 3 molds: Cranberry and Mango and Cherry." - end -end diff --git a/midterm/wish_list_spec.rb b/midterm/wish_list_spec.rb deleted file mode 100644 index 9ba4bce..0000000 --- a/midterm/wish_list_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "#{File.dirname(__FILE__)}/wish_list" - -describe WishList do - before :each do - @wish_list = WishList.new - @wish_list.wishes = ["Lamborghini", "Corn Starch and Water Moat", "Vegan Bacon Ice Cream", "Rubber Chicken", "Free Tickets to Spamalot"] - end - - it "should mixin Enumerable" do - @wish_list.is_a?(Enumerable).should be_true - end - - context "#each" do - it "should give me a numberd list" do - @wish_list.map{|w| w}.should eq ["1. Lamborghini", "2. Corn Starch and Water Moat", "3. Vegan Bacon Ice Cream", "4. Rubber Chicken", "5. Free Tickets to Spamalot"] - end - end -end \ No newline at end of file diff --git a/week1/homework/answers.txt b/week1/homework/answers.txt new file mode 100644 index 0000000..20ce5fe --- /dev/null +++ b/week1/homework/answers.txt @@ -0,0 +1,16 @@ +Please read: +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 an instance of a class. For example: “Back in Black” could be an object of the class “albums” + +2. What is a variable? A variable is a reference to an object and holds values. + +3. What is the difference between an object and a class? An object is an individual instance of a class. Objects are the data members of classes. Objects are also referred to as class instances. + +4. What is a String? Strings are sequence of characters stored in a variable. Strings can be printable characters or binary data. + +5. What are three messages that I can send to a string object? Hint: think methods: split, chomp and swapcase. + +6. What are two ways of defining a String literal? Bonus: What is the difference between them? +You can define them using single or double quotes. Double quotes gives you a lot more options for using escape sequences diff --git a/week1/homework/lor b/week1/homework/lor new file mode 100644 index 0000000..f6dfd5c --- /dev/null +++ b/week1/homework/lor @@ -0,0 +1,4 @@ +.... + +Finished in 0.00227 seconds +4 examples, 0 failures diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..f1382a1 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,23 @@ 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 deine empty string" do + @my_string.should_not be_empty + end + it "should be able to count the charaters" do + @my_string.should have(@my_string.length).characters + end + # ruby count string characters it "should be able to split on the . charater" do - pending - result = #do something with @my_string here - result.should have(2).items + # ruby split code + # + #result = @my_string.split(.) + #pending + #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"))' +# # encoding string +# @my_string.encoding.should eq (Encoding.find("("UTF-8").valid_encoding? end end end diff --git a/week2/homework/lor b/week2/homework/lor new file mode 100644 index 0000000..e69de29 diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..c46305c 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -1,13 +1,25 @@ -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? +A hashes are more flexible as you can index them using symbols, strings and regular expressions as opposed to just integers. + + 2. When would you use an Array over a Hash and vice versa? + I would use a hash if i needed to store the list using a key word or string for each variable. An array is simpler to set up and +simply uses an integer as it's key value pair based on the order that the variables are in the array. Arrays are easier to manipulate using integers. + + + +3. What is a module? Enumerable is a built in Ruby module, what is it? A module is a collection of methods, constants and classes. +Ennumberable is a built in mixin which provides various capabilities of sorting data. + + + +4. Can you inherit more than one thing in Ruby? How could you get around this problem? Ruby is a single inheritance language. +Mixins can be used within ruby classes to get around this limitation. + -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? A module can't have instances. A module is a chunk of reusable +code in it's own ruby program that i can call from another program using "requires". Modules can contain functions and/or variables. +Classes are like modules with the exception that you can use them to create new instances of each other without them interfering with each other. -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..025a9f5 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,22 @@ +module SimonSays + def echo(simon) + simon + end + + def shout(simon) + simon.upcase + end + + def start_of_word(simon,last) + simon[0...last] + end + + def first_word(simon) + simon.split.first + end + + def repeat simon, n=2 + results = (simon + ' ') * n + results.chop + end +end diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..25f697c 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -2,8 +2,9 @@ require "./simon_says.rb" describe SimonSays do - include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules) - + include SimonSays + +# include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules) # Hint: We are just calling methods, we are not passing a message to a SimonSays object. it "should echo hello" do echo("hello").should == "hello" diff --git a/week3/exercises/.book.rb.un~ b/week3/exercises/.book.rb.un~ new file mode 100644 index 0000000..e985a53 Binary files /dev/null and b/week3/exercises/.book.rb.un~ differ diff --git a/week3/exercises/.book_spec.rb.un~ b/week3/exercises/.book_spec.rb.un~ new file mode 100644 index 0000000..40067ed Binary files /dev/null and b/week3/exercises/.book_spec.rb.un~ differ diff --git a/week3/exercises/.human.rb.un~ b/week3/exercises/.human.rb.un~ new file mode 100644 index 0000000..614568c Binary files /dev/null and b/week3/exercises/.human.rb.un~ differ diff --git a/week3/exercises/.some_file.un~ b/week3/exercises/.some_file.un~ new file mode 100644 index 0000000..e28d5e5 Binary files /dev/null and b/week3/exercises/.some_file.un~ differ diff --git a/week3/exercises/.vampire.rb.un~ b/week3/exercises/.vampire.rb.un~ new file mode 100644 index 0000000..efb1a03 Binary files /dev/null and b/week3/exercises/.vampire.rb.un~ differ diff --git a/week3/exercises/.zombie.rb.un~ b/week3/exercises/.zombie.rb.un~ new file mode 100644 index 0000000..9406210 Binary files /dev/null and b/week3/exercises/.zombie.rb.un~ differ diff --git a/week3/exercises/book.rb b/week3/exercises/book.rb index c13e4d4..614350e 100644 --- a/week3/exercises/book.rb +++ b/week3/exercises/book.rb @@ -1,7 +1,25 @@ +$global_hello = "hi there" + +module Library + class Book - def pages - + attr_accessor :pages, :title + + @@library_count = 0 + + def self.library_count + @@library_count end + + def initialize pages, title="N/A" + @pages = pages + @title = title + @@library_count += 1 + end + -end \ No newline at end of file + def happy + $global_hello = "hello" + "There are #{@pages} happy pages in the book" + end diff --git a/week3/exercises/book_spec.rb b/week3/exercises/book_spec.rb index 72bc203..ea20250 100644 --- a/week3/exercises/book_spec.rb +++ b/week3/exercises/book_spec.rb @@ -1,10 +1,30 @@ require './book.rb' describe Book do - - it "should have a pages" do - book = Book.new - book.should respond_to "pages" - end -end \ No newline at end of file + 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 + Book.library_count.should eq 542 + 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 diff --git a/week3/exercises/human.rb b/week3/exercises/human.rb new file mode 100644 index 0000000..ccc32e5 --- /dev/null +++ b/week3/exercises/human.rb @@ -0,0 +1,6 @@ +require 'named_thing' + +class Human + include NamedThing + include OtherThing +end diff --git a/week3/exercises/some_file b/week3/exercises/some_file new file mode 100644 index 0000000..2224a72 --- /dev/null +++ b/week3/exercises/some_file @@ -0,0 +1,3 @@ +3 4 +5 6 +7 8 diff --git a/week3/exercises/vampire.rb b/week3/exercises/vampire.rb index 764adf6..6464111 100644 --- a/week3/exercises/vampire.rb +++ b/week3/exercises/vampire.rb @@ -3,4 +3,9 @@ class Vampire < Monster def initialize(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dangers=[:bites]) super(noc,legs,name,vul,dangers) end + + def bite! human + + end + end diff --git a/week3/exercises/zombie.rb b/week3/exercises/zombie.rb new file mode 100644 index 0000000..131600d --- /dev/null +++ b/week3/exercises/zombie.rb @@ -0,0 +1,8 @@ +require 'named_thing' +class Zombie + include NamedThing + + def say_name + "uuurrrgggghhhh #{@name}" + end +end diff --git a/week3/homework/.calculator.rb.swp b/week3/homework/.calculator.rb.swp new file mode 100644 index 0000000..daabfe0 Binary files /dev/null and b/week3/homework/.calculator.rb.swp differ diff --git a/week3/homework/.calculator.rb.un~ b/week3/homework/.calculator.rb.un~ new file mode 100644 index 0000000..c4928aa Binary files /dev/null and b/week3/homework/.calculator.rb.un~ differ diff --git a/week3/homework/.calculator_spec.rb.un~ b/week3/homework/.calculator_spec.rb.un~ new file mode 100644 index 0000000..010d1ab Binary files /dev/null and b/week3/homework/.calculator_spec.rb.un~ differ diff --git a/week3/homework/.questions.txt.un~ b/week3/homework/.questions.txt.un~ new file mode 100644 index 0000000..1663f27 Binary files /dev/null and b/week3/homework/.questions.txt.un~ differ diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..347f253 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,26 @@ +class Calculator + + def sum input + + total = 0 + input.each do |i| + total += i + end + total + + input.reduce 0, :+ + end + + def multiply *ary_nums + ary_nums.flatten.inject :* + end + + def pow base, exp + base**exp + end + + def fac n + multiply (1..n).to_a + end + +end diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..ab8cb8e 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,4 +1,4 @@ -require "#{File.dirname(__FILE__)}/calculator" +require './calculator.rb' describe Calculator do diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..0925dbf 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,29 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A symbol is a lot like a string in that it stores a chuck of data which can +be used multiple times through your program. + 2. What is the difference between a symbol and a string? +A string and symbol both store data. The difference is that while the data +in a string can change, the data in a symbol will always stay the same unless +it is overwritten. This is referred to as being immutable. However, a string +can be frozen giving it the characteristics of a symbol. The advantage to using +a symbol over a frozen string is in the way that the symbol is stored in memory +allowing for better program performance by using strings. -3. What is a block and how do I call a block? +3. What is a block and how do I call a block? Blocks are chunks of code that +are loaded while the ruby source is being executed or after the ruby code has +executed. The BEGIN blocks are exectued while the code is getting loaded and is +started wiht the BEGIN delclaration. The END blocks are executed after the program +has run and is started with the END declaration. There can be multiple BEGIN +and END blocks in a program. -4. How do I pass a block to a method? What is the method signature? +4. How do I pass a block to a method? What is the method signature? A block is +passed to a method by surrounding the block with curly braces after calling the +method. -5. Where would you use regular expressions? +5. Where would you use regular expressions? Regular expressions are used for +pattern matching. There are many expression patterns associated with expressions +which gives a wide variety of matching and manipulating data. diff --git a/week4/.code_timer_spec.rb.un~ b/week4/.code_timer_spec.rb.un~ new file mode 100644 index 0000000..809dd2d Binary files /dev/null and b/week4/.code_timer_spec.rb.un~ differ diff --git a/week4/class_materials/.code_timer.rb.un~ b/week4/class_materials/.code_timer.rb.un~ new file mode 100644 index 0000000..0f6ea9a Binary files /dev/null and b/week4/class_materials/.code_timer.rb.un~ differ diff --git a/week4/class_materials/.code_timer_spec.rb.un~ b/week4/class_materials/.code_timer_spec.rb.un~ new file mode 100644 index 0000000..1da7ce3 Binary files /dev/null and b/week4/class_materials/.code_timer_spec.rb.un~ differ diff --git a/week4/class_materials/code_timer.rb b/week4/class_materials/code_timer.rb new file mode 100644 index 0000000..3403518 --- /dev/null +++ b/week4/class_materials/code_timer.rb @@ -0,0 +1,9 @@ +class CodeTimer + + def self.time_code n=1 + start = Time.now + n.times {yield} + Time.now - start + end + +end diff --git a/week4/class_materials/code_timer_spec.rb b/week4/class_materials/code_timer_spec.rb new file mode 100644 index 0000000..e2701db --- /dev/null +++ b/week4/class_materials/code_timer_spec.rb @@ -0,0 +1,31 @@ +require './code_timer' + +describe CodeTimer do + + it "should run our code" do + + flag = false + CodeTimer.time_code do + flag = true + end + flag.should be_true + end + + it "should time our code" do + Time.stub(:now).and_return(0,3) + time = CodeTimer.time_code do + end + time.should be_within(0.1).of(3.0) + + end + + it "should run our code multiple times" do + count = 0 + CodeTimer.time_code 100 do + count += 1 + puts count + end + count.should eq 100 + end + +end diff --git a/week4/homework/.questions.txt.un~ b/week4/homework/.questions.txt.un~ new file mode 100644 index 0000000..5551ae3 Binary files /dev/null and b/week4/homework/.questions.txt.un~ differ diff --git a/week4/homework/.worker.rb.un~ b/week4/homework/.worker.rb.un~ new file mode 100644 index 0000000..4d9084a Binary files /dev/null and b/week4/homework/.worker.rb.un~ differ diff --git a/week4/homework/jack b/week4/homework/jack new file mode 100644 index 0000000..3b18e51 --- /dev/null +++ b/week4/homework/jack @@ -0,0 +1 @@ +hello world diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..d687eac 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,12 +3,33 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby reads files by opening the file with File.open and reading the +file with File.gets. You do not have to implicitly close the file - +that is done automatically at the end of the block. 2. How would you output "Hello World!" to a file called my_output.txt? +>> File.open("my_output.txt", "w") do |file| +?> file.puts "hello world" +>> end +=> nil +>> quit +m08642391% cat my_output.txt +hello world 3. What is the Directory class and what is it used for? +I'm not sure about this one. The only thing i found was a Dir class which +does things like list, change, create, delete directories. I used the following +code to output my current directory: + +> puts Dir.pwd +/Users/xok6/class/RubyWinter2014/week4/homework +=> nil 4. What is an IO object? +IO object is a class which handles input and output. It's a bidirectional +channel between a Ruby program an external resource. 5. What is rake and what is it used for? What is a rake task? - +rake is a gem for building ruby programs. Tasks are set up in a Rakefile. +These tasks are the build rules which tells your build what to do and which +files are involved with your build. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..9612ca5 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,7 @@ +class Worker + + def self.work n = 1 + n.times.inject(nil){yield} + end + +end diff --git a/week5/Rakefile.rb b/week5/Rakefile.rb new file mode 100644 index 0000000..5c06720 --- /dev/null +++ b/week5/Rakefile.rb @@ -0,0 +1,5 @@ +task :default => [:say_hello] +desc "output to world" +task :say_hello do + puts "hello world" +end diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..add77aa --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,20 @@ +task :default => [:test3] + +task :test1 do + File.open("names") do |file| + while line = file.gets + puts line + end + end +end +task :test2 do + Dir.mkdir("class") unless Dir.exists? "class" +end +task :test3 do + Dir.mkdir("class") # unless Dir.exists? "class" + File.open("names") do |file| + while line = file.gets + Dir.mkdir line.chomp unless file.directory? line.chomp + end + end +end diff --git a/week5/test b/week5/test new file mode 100644 index 0000000..b6067b2 --- /dev/null +++ b/week5/test @@ -0,0 +1,25 @@ +asfdfsda +fads +fads +afsd +asfd +fascda +sfasd +asdf +dasf +fads +c +fdsa +fdsa +fdsa +fdsa +fds +fsa +fasd +fdsa +dfas +fdsa +fads +fds +fdas +fda diff --git a/week7/exercises/features/celsius.feature b/week7/exercises/features/celsius.feature new file mode 100644 index 0000000..9a53384 --- /dev/null +++ b/week7/exercises/features/celsius.feature @@ -0,0 +1,16 @@ +Feature: Converting metric + In order to pack for London + As a traveler + I want to be told the Celsius temperature in Fahrenheit + +Scenario: + Given I have entered 0 into the converter + And I set the type to "Celsius" + When I press convert + Then the result returned should be 32.0 + +Scenario: + Given I have entered 21 into the converter + And I set the type to "Celsius" + When I press convert + Then the result returned should be 69.8 diff --git a/week7/exercises/features/converter.feature b/week7/exercises/features/fahrenheit.feature similarity index 75% rename from week7/exercises/features/converter.feature rename to week7/exercises/features/fahrenheit.feature index 5e262a8..7e3c0ff 100644 --- a/week7/exercises/features/converter.feature +++ b/week7/exercises/features/fahrenheit.feature @@ -5,13 +5,12 @@ Feature: Converting metric Scenario: Given I have entered 32 into the converter - And I set the type to Fahrenheit + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 0.0 Scenario: Given I have entered 75 into the converter - And I set the type to Fahrenheit + And I set the type to "Fahrenheit" When I press convert - Then the result returned should be 23.9 - + Then the result returned should be 23.9 diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb new file mode 100644 index 0000000..ab60db0 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter.rb @@ -0,0 +1,19 @@ +class Converter + attr_accessor :type + def initialize value + @value = value.to_f + end + + def convert + send "#{@type}_converter" + + end + private + def Fahrenheit_converter + ((@value - 32.0) * (5.0/9.0)).round(1) + end + + def Celsius_converter + (@value * (9.0/5.0) + 32.0).round(1) + end +end diff --git a/week7/exercises/features/step_definitions/converter_steps.rb b/week7/exercises/features/step_definitions/converter_steps.rb new file mode 100644 index 0000000..14ecd86 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter_steps.rb @@ -0,0 +1,15 @@ +Given(/^I have entered (\d+) into the converter$/) do |arg1| + @converter = Converter.new arg1 +end + +Given(/^I set the type to "(.*?)"$/) do |type| + @converter.type = type +end + +When(/^I press convert$/) do + @result = @converter.convert +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + @result.should eq "#{arg1}.#{arg2}".to_f +end diff --git a/week7/features/cool_game.feature b/week7/features/cool_game.feature new file mode 100644 index 0000000..ce84882 --- /dev/null +++ b/week7/features/cool_game.feature @@ -0,0 +1,13 @@ +Feature: This is a really cool game. + +Scenario: Jack always wins the game. + Given "Jack" is logged in + When he clicks move + Then he wins the game + And he sees the text "You Won!" + + Scenario: Owen always loses the game. + Given "Owen" is logged in + When he clicks move + Then he loses the game + And he sees the text "You Lost!" diff --git a/week7/features/step_definitions/cool_game.rb b/week7/features/step_definitions/cool_game.rb new file mode 100644 index 0000000..d4a19ff --- /dev/null +++ b/week7/features/step_definitions/cool_game.rb @@ -0,0 +1,25 @@ +class CoolGame + def initialize name + @player = name + end + + def move + if @player == "Jack" + @won = true + else + @won = false + end + end + + def won? + @won + end + + def output + "You #{won? ? 'Won' : 'Lost'}!" + end + + def lose? + + end +end diff --git a/week7/features/step_definitions/cool_game_steps.rb b/week7/features/step_definitions/cool_game_steps.rb new file mode 100644 index 0000000..78652ca --- /dev/null +++ b/week7/features/step_definitions/cool_game_steps.rb @@ -0,0 +1,19 @@ +Given(/^"(.*?)" is logged in$/) do |name| + @game = CoolGame.new(name) +end + +When(/^he clicks move$/) do + @game.move +end + +Then(/^he wins the game$/) do + @game.won?.should be_true +end + +Then(/^he loses the game$/) do + @game.lose?.should be_false +end + +Then(/^he sees the text "(.*?)"$/) do |arg1| + @game.output.should eq arg1 +end diff --git a/week7/homework/features.orig/pirate.feature b/week7/homework/features.orig/pirate.feature new file mode 100644 index 0000000..7de1a0b --- /dev/null +++ b/week7/homework/features.orig/pirate.feature @@ -0,0 +1,11 @@ +# language: en-pirate + +Ahoy matey!: Pirate Speak + I would like help to talk like a pirate + +Heave to: The mighty speaking pirate + Gangway! I have a PirateTranslator + Blimey! I say 'Hello Friend' + Aye I hit translate + Let go and haul it prints out 'Ahoy Matey' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features.orig/step_definitions/pirate_steps.rb b/week7/homework/features.orig/step_definitions/pirate_steps.rb new file mode 100644 index 0000000..faf1a7f --- /dev/null +++ b/week7/homework/features.orig/step_definitions/pirate_steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a (\w+)$/ do |arg| + @translator = Kernel.const_get(arg).new +end + +Blimey /^I (\w+) '(.+)'$/ do |method, arg| + @translator.send(method, arg) +end + +Letgoandhaul /^I hit (\w+)$/ do |arg| + @result = @translator.send(arg) +end + +Letgoandhaul /^it prints out '(.+)'$/ do |arg| + @result.split("\n ").first.should == arg +end + +Letgoandhaul /^it also prints '(.+)'$/ do |arg| + @result.split("\n ").last.should == arg +end diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features.orig/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/features.orig/step_definitions/tic-tac-toe-steps.rb diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features.orig/tic-tac-toe.feature similarity index 100% rename from week7/homework/features/tic-tac-toe.feature rename to week7/homework/features.orig/tic-tac-toe.feature diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..7384ae3 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,18 @@ +#class String +# def to_pirate_symbol +# self.downcase.gsub(' ', '_').intern +# end +#end + +class PirateTranslator + puts "Hello Friend" +end + +def say something + @said = something +end + +def translate + jack = [ 'Ahoy Matey', 'Shiber Me Timbers You Scurvey Dogs!!' ] + p "#{jack[0]}" + "\n #{jack[1]}" +end diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..0aa06e8 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -8,6 +8,7 @@ Letgoandhaul /^I hit (\w+)$/ do |arg| @result = @translator.send(arg) + puts "#{arg}" end Letgoandhaul /^it prints out '(.+)'$/ do |arg| @@ -15,5 +16,6 @@ end Letgoandhaul /^it also prints '(.+)'$/ do |arg| + puts "#{arg}" @result.split("\n ").last.should == arg end diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..d068fb7 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -2,8 +2,55 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: -1. What is method_missing and how can it be used? +1. What is method_missing and how can it be used? +Method missing is a method that you can define as a default method to use if an +object uses a method that is undefined. + +eg: +M08642391% irb +>> class Jack +>> def method_missing(name, *args) +>> puts "Jack is awesome at #{name}: #{args[0]}" +>> end +>> end +=> nil +>> jack = Jack.new +=> # +>> jack.ruby("RUBY!!!") +Jack is awesome at ruby: RUBY!!! +=> nil +-------------------------------------------------------------------------------- 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +Eigenclass is another name for the singleton class and are used for defining methods +that are specific to a particular object. + +M08642391% irb +>> class Jack +>> end +=> nil +>> jack=Jack.new +=> # +>> def jack.atbat +>> puts "Jack hit a home run!!" +>> end +=> nil +>> jack.atbat +Jack hit a home run!! + +-------------------------------------------------------------------------------- 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? +Ducktyping is writing code that works with objects regardless of what type the +object is. Opting to use ducktyping can reduce the amount of code and in some +case can improve performance to your application. +-------------------------------------------------------------------------------- +4. What is the difference between a class method and an instance method? What is +the difference between instance_eval and class_eval? +the difference between a class method and an instance method is that instant +method works on normal objacts and class methods work on class objects. +The instance_eval can be used to perform immediate evalualtion on an object. +The class_eval works the same way, but the method works as though it were in a +class. +-------------------------------------------------------------------------------- 5. What is the difference between a singleton class and a singleton method? +A singleton method is a method defined for a single oject and a singleton class +is a method defined for an entire class.