This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Week 7 homework for Chris Montes #110
Open
mandelbro
wants to merge
14
commits into
UWE-Ruby:master
Choose a base branch
from
mandelbro:week-7-homework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
660ea8a
Adds info for Chris Montes
b8847be
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
ce04b63
Adds in class work
cd660a1
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
0ae6dba
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
86c9767
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
cb5e5e7
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
13a2997
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
ac1277c
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
adc16df
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
b55eeb3
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2013
dc37ef9
Adds converter answers for week 7 homework
c275e05
Adds PirateTranslator class for week 7 homework
f4ca2a6
Adds question responses from reading for week 7 homework
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
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.0) / (9.0/5.0)).round(1) | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
week7/exercises/features/step_definitions/converter_steps.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
16 changes: 16 additions & 0 deletions
16
week7/homework/features/step_definitions/piarate_translator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
class PirateTranslator | ||
|
||
@@dictionary = { | ||
"Hello Friend" => "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!" | ||
} | ||
|
||
def say(text) | ||
@text = text | ||
end | ||
|
||
def translate | ||
@@dictionary[@text] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,23 @@ | |
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 Ruby hook method that runs when a method has been sent to an object that is unsupported by it or it's parent classes. Essentially it is used to handle calls to undefined methods, which is useful for implementing features of a dynamic frame, like objects with dynamic accessors, i.e., allowing users to dynamically add properties to a class instance. Another implementation would be providing optional methods or hook callbacks withing a framework or CMS. | ||
|
||
2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? | ||
|
||
Eiganclass is an anonymous class created to when a singleton method is added to an object instance. The Eigenclass class extends the object's original class, and the object becomes an instance of the Eigenclass. The singelton method lives inside this class. A common implementation of this is with macros like 'attr_accessor' or 'has_many' relationship macro in Rails, where the macro defines singleton methods based on the arguments passed to it. | ||
|
||
3. When would you use DuckTypeing? How would you use it to improve your code? | ||
|
||
Ducktyping is useful in dynamic typed languages, where objects are defined by their purpose rather than their type. I would use it to make my functions more robust, i.e., better able to handle a wide array of inputs, more simple, one or two lines of code rather than complicated if/else or switch statements, and to take advantage of making my class more efficient and easy to use by allowing users to pass, say, entire loaded objects OR numeric or string identifies. | ||
|
||
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 methods that can be called directly on classes, e.g., ClassName.new, but not by instances of a class, while instance methods only be called on an instance of a class, e.g., object.doSomething, but not on the class itself. The methods 'instance_eval' defines class methods, while 'class_eval' defines instance methods. As you would expect. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They both allow you to change the identity of 'self' for the duration of a block. class_eval sets things up as if you were in the body of a class definition. instance_eval acts as though you were working inside the singleton class of 'self'. |
||
|
||
5. What is the difference between a singleton class and a singleton method? | ||
|
||
A singleton method is a method of the singleton class, the class that is created to support the singleton method when the method is defined. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good!