From 332ea4d546565fae1611b7e878fe57c8cfcc6e49 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 26 Nov 2013 15:38:12 -0800 Subject: [PATCH 1/2] Sean Sehr's week7 homework --- .../step_definitions/pirate_translator.rb | 17 +++++++++++++++++ week7/homework/questions.txt | 9 +++++++++ 2 files changed, 26 insertions(+) create mode 100644 week7/homework/features/step_definitions/pirate_translator.rb 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..04631d0 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_translator.rb @@ -0,0 +1,17 @@ +class PirateTranslator + attr_accessor :phrases + + def initialize + @phrases = { + "Hello Friend" => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" + } + end + + def say text + @text = text + end + + def translate + @phrases[@text] + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..40e5d59 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,16 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +The method_messing is used when an object executes the method, but the method is not found. It is ran instead of a NoMethodError exception. The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An Eigenclass is the anonymous class that is created when you define a Singleton method on an object. It is used to hold the new method and the original class as the superclass.Singleton methods live inside this Eigenclass + 3. When would you use DuckTypeing? How would you use it to improve your code? +I would use DuckTypeing when working in dynamic languages that do not have strong typing (Ruby, Javascript, etc). I would use to make my code more flexable and futureproof. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +Class methods are ran on the class themselves while instances methods are ran on that particular instance. If you create a new instance of a class the class method will not work. Attempting to run a class method on an instance will also not work. A class method is for anything that does not deal with an individual instance of a class. instance_eval is used to dfine a class method while class_eval is used ot define an instance method. + 5. What is the difference between a singleton class and a singleton method? +A singleton class effects all instances of that class, a singleton method only effects the single instance. From aa0e9fbd13af8796194eab8363521989b898db47 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 26 Nov 2013 15:44:27 -0800 Subject: [PATCH 2/2] adding converter spec --- .../features/step_definitions/converter.rb | 18 ++++++++++++++++++ .../step_definitions/converter_steps.rb | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 week7/exercises/features/step_definitions/converter.rb create mode 100644 week7/exercises/features/step_definitions/converter_steps.rb diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb new file mode 100644 index 0000000..48177c7 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter.rb @@ -0,0 +1,18 @@ +class Converter + + def initialize(unit) + @unit = unit.to_f + end + + def type=(type) + @type = type + end + + def convert + self.send("#{@type}_convertion") + end + + def Fahrenheit_convertion + ((@unit - 32) * 5/9).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..e5ae467 --- /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 Fahrenheit$/) do + @converter.type = "Fahrenheit" +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