-
Notifications
You must be signed in to change notification settings - Fork 9
Panda Complete #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
var114
wants to merge
5
commits into
RubyoffRails:master
Choose a base branch
from
var114:master
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
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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,23 @@ | ||
| class WidgetFactory | ||
| def self.create!(klass) | ||
| klass.class_eval do | ||
| define_method :way_cool do | ||
| 3.times {puts "My name is..."} | ||
| puts klass.name | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| module NamingAwesomeness | ||
|
|
||
| def self.included(klass) #gets called when you include a class | ||
| WidgetFactory.create!(klass) | ||
| end | ||
| end | ||
|
|
||
| class CompileTime | ||
| include NamingAwesomeness | ||
| end | ||
|
|
||
| CompileTime.new.way_cool | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,45 +1,50 @@ | ||
| # add a method at run time | ||
| # add a method at run time | ||
| class SuperWo | ||
|
|
||
| def initialize(name) | ||
| @name = name | ||
| end | ||
| def initialize(name) | ||
| @name = name | ||
| end | ||
|
|
||
| def add_method_to_instance | ||
| instance_eval do | ||
| def cool! | ||
| puts "oh hai, I am the cool" | ||
| end | ||
| end | ||
| def add_method_to_instance | ||
| instance_eval do | ||
| def cool! | ||
| puts "oh hello" | ||
| end | ||
| end | ||
|
|
||
| def add_method_to_class | ||
| self.class.class_eval do | ||
| define_method :cool! do | ||
| puts "oh hai, I am the cool" | ||
| end | ||
|
|
||
| #this is not being add to the entire class, | ||
| #instance only available to the methd. | ||
| def add_method_to_class | ||
| self.class.class_eval do | ||
| define_method :cool! do | ||
| puts "hello, world" | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def add_some_code_to_class(the_name, &block) | ||
| self.class.class_eval do | ||
| define_method the_name, block | ||
| self.class.class_eval do #powering the meta | ||
| define_method the_name, &block | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| wo = SuperWo.new("first") | ||
| wo.add_method_to_instance | ||
| wo.cool! | ||
|
|
||
| second_wo = SuperWo.new("second") | ||
| second_wo.add_method_to_instance | ||
| second_wo.cool! | ||
| =begin | ||
| x = SuperWo.new | ||
| x.add_method_to_class | ||
| x.cool! | ||
|
|
||
| y = SuperWo.new | ||
| y.cool! | ||
| =end | ||
|
|
||
| third_wo = SuperWo.new("blue bell") | ||
| third_wo.add_some_code_to_class :wow_wow do | ||
| puts "#{@name} I am wow" | ||
| end | ||
| third_wo.wow_wow | ||
|
|
||
| # able to access intance methods in the class | ||
|
|
This file contains hidden or 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,13 @@ | ||
| require "rubygems" | ||
| require "bundler/setup" | ||
|
|
||
| require 'rspec/core/rake_task' | ||
|
|
||
| desc 'Default: run specs.' | ||
| task :default => :spec | ||
|
|
||
| desc "Run specs" | ||
| RSpec::Core::RakeTask.new do |t| | ||
| t.pattern = "**/*_spec.rb" # don't need this, it's default. | ||
| # Put spec opts in a file named .rspec in root | ||
| end |
This file contains hidden or 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,64 @@ | ||
|
|
||
| require 'rspec' | ||
| require 'rake' | ||
| require "./terminator" | ||
|
|
||
|
|
||
| describe GoodTerminator do | ||
| it "should protect_john_connor" do | ||
| subject.protect_john_connor! | ||
| subject.current_mission.should eq("protect: john_connor") | ||
| end | ||
|
|
||
| it "should protect_sarah_connor" do | ||
| subject.protect_sarah_connor! | ||
| subject.current_mission.should eq("protect: sarah_connor") | ||
| end | ||
|
|
||
| it "should know if the Terminator protects a person" do | ||
| subject.protect_sarah_connor! | ||
| subject.protect?("sarah_connor").should eq(true) | ||
| end | ||
|
|
||
| it "should know if the Terminator is good" do | ||
| subject.protect_sarah_connor! | ||
| subject.good?.should eq(true) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| describe BadTerminator do | ||
| it "should destroy_john_connor" do | ||
| subject.destroy_john_connor! | ||
| subject.current_mission.should eq("destroy: john_connor") | ||
| end | ||
|
|
||
| it "should destroy_sarah_connor" do | ||
| subject.destroy_sarah_connor! | ||
| subject.current_mission.should eq("destroy: sarah_connor") | ||
| end | ||
|
|
||
| it "should know if the Terminator destroys a person" do | ||
| subject.destroy_sarah_connor! | ||
| subject.protect?("sarah_connor").should eq(false) | ||
| end | ||
|
|
||
| it "should know if the Terminator is bad" do | ||
| subject.destroy_sarah_connor! | ||
| subject.good?.should eq(false) | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| require "./terminatorable" | ||
|
|
||
| class Terminator | ||
|
|
||
| include Terminatorable | ||
| likes_to_protect [:john_connor, :sarah_connor] | ||
| good | ||
|
|
||
| def protect?(name) | ||
| current_mission == "protect: #{name}" | ||
| end | ||
|
|
||
| end | ||
|
|
||
| class GoodTerminator < Terminator | ||
| likes_to_protect [{protect: [:john_connor, :sarah_connor] , destroy: [:john_connor, :sarah_connor]}] | ||
| likes_to_destroy [{protect: [:john_connor, :sarah_connor] , destroy: [:john_connor, :sarah_connor]}] | ||
| end | ||
|
|
||
| class BadTerminator < Terminator | ||
| likes_to_protect [{protect: [:john_connor, :sarah_connor] , destroy: [:john_connor, :sarah_connor]}] | ||
| likes_to_destroy [{protect: [:john_connor, :sarah_connor] , destroy: [:john_connor, :sarah_connor]}] | ||
| end | ||
|
|
||
|
|
||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -1,23 +1,64 @@ | ||
|
|
||
| require 'rspec' | ||
| require 'rake' | ||
| require "./terminator" | ||
|
|
||
| describe Terminator do | ||
| it "should destroy_john_connor" do | ||
| subject.destroy_john_connor! | ||
| subject.current_mission.should eq("destroy: john_connor") | ||
| end | ||
|
|
||
| it "should protect_john_connor" do | ||
| subject.protect_john_connor! | ||
| subject.current_mission.should eq("protect: john_connor") | ||
| end | ||
| it "should destroy_sarah_connor" do | ||
| subject.destroy_sarah_connor! | ||
| subject.current_mission.should eq("destroy: sarah_connor") | ||
| describe GoodTerminator do | ||
| it "should protect_john_connor" do | ||
| subject.protect_john_connor! | ||
| subject.current_mission.should eq("protect: john_connor") | ||
| end | ||
|
|
||
| it "should protect_sarah_connor" do | ||
| subject.protect_sarah_connor! | ||
| subject.current_mission.should eq("protect: sarah_connor") | ||
| end | ||
|
|
||
| it "should know if the Terminator protects a person" do | ||
| subject.protect_sarah_connor! | ||
| subject.protect?("sarah_connor").should eq(true) | ||
| end | ||
|
|
||
| it "should know if the Terminator is good" do | ||
| subject.protect_sarah_connor! | ||
| subject.good?.should eq(true) | ||
| end | ||
| end | ||
|
|
||
| it "should protect_sarah_connor" do | ||
| subject.protect_sarah_connor! | ||
| subject.current_mission.should eq("protect: sarah_connor") | ||
|
|
||
| describe BadTerminator do | ||
| it "should destroy_john_connor" do | ||
| subject.destroy_john_connor! | ||
| subject.current_mission.should eq("destroy: john_connor") | ||
| end | ||
|
|
||
| it "should destroy_sarah_connor" do | ||
| subject.destroy_sarah_connor! | ||
| subject.current_mission.should eq("destroy: sarah_connor") | ||
| end | ||
|
|
||
| it "should know if the Terminator destroys a person" do | ||
| subject.destroy_sarah_connor! | ||
| subject.protect?("sarah_connor").should eq(false) | ||
| end | ||
|
|
||
| it "should know if the Terminator is bad" do | ||
| subject.destroy_sarah_connor! | ||
| subject.good?.should eq(false) | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -1,19 +1,41 @@ | ||
|
|
||
|
|
||
| # when you include a module into a Ruby class, | ||
| # the instance methods in that module becomes | ||
| # availabe as instance methods of the class. | ||
| # multiple classes can inherit from a single module | ||
|
Member
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. Excellent definition. Also, a class can inherit multiple modules. witness: module Calcable
def multiply(x, y)
x + y
end
end
module FortyTwo
def multiply(x,y)
42
end
end
class Device
include Calcable
include FortyTwo
end
Device.new.multiply(5, 20)Check out here for the answer: http://rubyfiddle.com/riddles/3b96d |
||
|
|
||
| module Terminatorable | ||
|
|
||
| module ClassMethods | ||
| def likes_to_protect(people=[]) | ||
| ["destroy", "protect"].each do |mission_type| | ||
| people.each do |person| | ||
| define_method "#{mission_type}_#{person}!" do | ||
| @current_mission = "#{mission_type}: #{person}" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def likes_to_protect(people={}) | ||
| people.first.fetch(:protect).each do |person| | ||
| #people.each do |person| | ||
| define_method "protect_#{person}!" do | ||
| @current_mission = "protect: #{person}" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def likes_to_destroy(people=[]) | ||
| people.first.fetch(:destroy).each do |person| | ||
| define_method "destroy_#{person}!" do | ||
| @current_mission = "destroy: #{person}" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def good | ||
| define_method :good? do | ||
| @current_mission.include?("protect") | ||
|
|
||
| end | ||
| end | ||
| end | ||
|
|
||
| def self.included(klass) | ||
| attr_reader :current_mission | ||
| klass.extend Terminatorable::ClassMethods | ||
| attr_reader :current_mission | ||
| klass.extend Terminatorable::ClassMethods | ||
| end | ||
| end | ||
| end | ||
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.
This widget factory ROCKS. 🎉 👍