Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Week7 homework for Sean Sehr #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions week7/exercises/features/step_definitions/converter.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions week7/exercises/features/step_definitions/converter_steps.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions week7/homework/features/step_definitions/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions week7/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!


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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A singleton method is a method that is specific to a particular object (as opposed to a class).
The singleton class, or Eigneclass, is an anonymous class that bursts into appearance when a singleton method is defined. It holds the definition of the singleton method. It becomes the class of the object that the singleton method is attached to, pushing that object's original class up a level to become its superclass.

A singleton class effects all instances of that class, a singleton method only effects the single instance.