From 00d6d052a8fe87d49a5a1f1b77d5b0b28a32fe1a Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Mon, 6 Jan 2014 19:53:49 -0800 Subject: [PATCH 1/5] thus far --- README.md | 2 +- compiletime.rb | 23 +++++++++++++++++++ meta.rb | 55 +++++++++++++++++++++++++--------------------- terminator.rb | 2 +- terminator_spec.rb | 10 ++++++--- terminatorable.rb | 22 +++++++++++++------ 6 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 compiletime.rb diff --git a/README.md b/README.md index 860c40c..526aeb3 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Meta Programming in Ruby # #class BadTerminator # likes_to_protect [] -# likes_to_destroy [:john_connor, :sarah_connor] +### likes_to_destroy [:john_connor, :sarah_connor] #end ``` diff --git a/compiletime.rb b/compiletime.rb new file mode 100644 index 0000000..3a5e114 --- /dev/null +++ b/compiletime.rb @@ -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 \ No newline at end of file diff --git a/meta.rb b/meta.rb index ae8f9b9..56aa042 100644 --- a/meta.rb +++ b/meta.rb @@ -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 + diff --git a/terminator.rb b/terminator.rb index 8bd4a43..72544fc 100644 --- a/terminator.rb +++ b/terminator.rb @@ -3,5 +3,5 @@ class Terminator include Terminatorable likes_to_protect [:john_connor, :sarah_connor] -end +end \ No newline at end of file diff --git a/terminator_spec.rb b/terminator_spec.rb index 749dae3..f42b5d3 100644 --- a/terminator_spec.rb +++ b/terminator_spec.rb @@ -1,20 +1,24 @@ + require 'rspec' require "./terminator" + + describe Terminator do it "should destroy_john_connor" do subject.destroy_john_connor! subject.current_mission.should eq("destroy: john_connor") - end + 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 + + it "should destroy_sarah_connor" do subject.destroy_sarah_connor! subject.current_mission.should eq("destroy: sarah_connor") - end + end it "should protect_sarah_connor" do subject.protect_sarah_connor! diff --git a/terminatorable.rb b/terminatorable.rb index 378afd6..b5c061a 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -1,19 +1,27 @@ + + +# 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 + module Terminatorable module ClassMethods - def likes_to_protect(people=[]) + + def likes_to_protect(people=[]) ["destroy", "protect"].each do |mission_type| people.each do |person| - define_method "#{mission_type}_#{person}!" do + define_method "#{mission_type}_#{person}!" do @current_mission = "#{mission_type}: #{person}" - end + end end - end + 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 \ No newline at end of file From 71dbef6621c2a6ffcd2ef35704e8e74ac4f71ae0 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 7 Jan 2014 16:38:30 -0800 Subject: [PATCH 2/5] panda --- terminator.rb | 18 +++++++++++++++++- terminator_spec.rb | 26 ++++++++++++++++++++++++-- terminatorable.rb | 10 ++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/terminator.rb b/terminator.rb index 72544fc..cd79c85 100644 --- a/terminator.rb +++ b/terminator.rb @@ -3,5 +3,21 @@ class Terminator include Terminatorable likes_to_protect [:john_connor, :sarah_connor] + good + + def protect?(name) + current_mission == "protect: #{name}" + end + +end +#class GoodTerminator +# likes_to_protect [:john_connor, :sarah_connor] +# likes_to_destroy [] +#end + +#class BadTerminator +# likes_to_protect [] +# likes_to_destroy [:john_connor, :sarah_connor] +#end + -end \ No newline at end of file diff --git a/terminator_spec.rb b/terminator_spec.rb index f42b5d3..995382e 100644 --- a/terminator_spec.rb +++ b/terminator_spec.rb @@ -1,9 +1,8 @@ require 'rspec' +#require 'rake' require "./terminator" - - describe Terminator do it "should destroy_john_connor" do subject.destroy_john_connor! @@ -24,4 +23,27 @@ 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 destroys a person" do + subject.destroy_sarah_connor! + subject.protect?("sarah_connor").should eq(false) + end + + it "should know if the Terminator is good" do + subject.protect_sarah_connor! + subject.good?.should eq(true) + end + + it "should know if the Terminator is bad" do + subject.destroy_sarah_connor! + subject.good?.should eq(false) + end end + + + diff --git a/terminatorable.rb b/terminatorable.rb index b5c061a..eda9f47 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -18,6 +18,16 @@ def likes_to_protect(people=[]) end end end + + def good + define_method :good? do + if @current_mission.include?("protect") + true + else + false + end + end + end end def self.included(klass) From 36acbba0ce57465b96f2d61a285323324bda6613 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 7 Jan 2014 17:19:05 -0800 Subject: [PATCH 3/5] tiger, eagle --- terminator.rb | 18 ++++++++--------- terminator_spec.rb | 49 ---------------------------------------------- terminatorable.rb | 34 +++++++++++++++++++------------- 3 files changed, 29 insertions(+), 72 deletions(-) delete mode 100644 terminator_spec.rb diff --git a/terminator.rb b/terminator.rb index cd79c85..fc9313d 100644 --- a/terminator.rb +++ b/terminator.rb @@ -2,7 +2,6 @@ class Terminator include Terminatorable - likes_to_protect [:john_connor, :sarah_connor] good def protect?(name) @@ -10,14 +9,15 @@ def protect?(name) end end -#class GoodTerminator -# likes_to_protect [:john_connor, :sarah_connor] -# likes_to_destroy [] -#end + class GoodTerminator < Terminator + likes_to_protect [:john_connor, :sarah_connor] + likes_to_destroy [] + end + + class BadTerminator < Terminator + likes_to_protect [] + likes_to_destroy [:john_connor, :sarah_connor] + end -#class BadTerminator -# likes_to_protect [] -# likes_to_destroy [:john_connor, :sarah_connor] -#end diff --git a/terminator_spec.rb b/terminator_spec.rb deleted file mode 100644 index 995382e..0000000 --- a/terminator_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ - -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") - 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 destroys a person" do - subject.destroy_sarah_connor! - subject.protect?("sarah_connor").should eq(false) - end - - it "should know if the Terminator is good" do - subject.protect_sarah_connor! - subject.good?.should eq(true) - end - - it "should know if the Terminator is bad" do - subject.destroy_sarah_connor! - subject.good?.should eq(false) - end -end - - - diff --git a/terminatorable.rb b/terminatorable.rb index eda9f47..24e930e 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -10,25 +10,31 @@ 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 + people.each do |person| + define_method "protect_#{person}!" do + @current_mission = "protect: #{person}" + end + end end - def good - define_method :good? do - if @current_mission.include?("protect") - true - else - false - end + def likes_to_destroy(people=[]) + people.each do |person| + define_method "destroy_#{person}!" do + @current_mission = "destroy: #{person}" + end + end + end + + def good + define_method :good? do + if @current_mission.include?("protect") + true + else + false end end end +end def self.included(klass) attr_reader :current_mission From 875a2ccbf02f96a0bb11c607a03a03033c6111b6 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 7 Jan 2014 17:24:49 -0800 Subject: [PATCH 4/5] tiger, eagle --- rakefile.rb | 13 +++++++++ spec/terminator_spec.rb | 64 +++++++++++++++++++++++++++++++++++++++++ terminator_spec.rb | 64 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 rakefile.rb create mode 100644 spec/terminator_spec.rb create mode 100644 terminator_spec.rb diff --git a/rakefile.rb b/rakefile.rb new file mode 100644 index 0000000..a547a71 --- /dev/null +++ b/rakefile.rb @@ -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 diff --git a/spec/terminator_spec.rb b/spec/terminator_spec.rb new file mode 100644 index 0000000..a00b2ec --- /dev/null +++ b/spec/terminator_spec.rb @@ -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 + + + + + + + + + + + + + + diff --git a/terminator_spec.rb b/terminator_spec.rb new file mode 100644 index 0000000..a00b2ec --- /dev/null +++ b/terminator_spec.rb @@ -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 + + + + + + + + + + + + + + From 37169e5d3d8757d385a5d38b67a736b757694f5c Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Wed, 8 Jan 2014 14:17:22 -0800 Subject: [PATCH 5/5] fixes --- terminator.rb | 12 +++++++----- terminatorable.rb | 14 ++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/terminator.rb b/terminator.rb index fc9313d..99e287f 100644 --- a/terminator.rb +++ b/terminator.rb @@ -1,22 +1,24 @@ require "./terminatorable" class Terminator + include Terminatorable - good + good def protect?(name) current_mission == "protect: #{name}" end end + class GoodTerminator < Terminator - likes_to_protect [:john_connor, :sarah_connor] - likes_to_destroy [] + 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 [] - likes_to_destroy [:john_connor, :sarah_connor] + 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 diff --git a/terminatorable.rb b/terminatorable.rb index 24e930e..5d230f9 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -9,8 +9,9 @@ module Terminatorable module ClassMethods - def likes_to_protect(people=[]) - people.each do |person| + 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 @@ -18,7 +19,7 @@ def likes_to_protect(people=[]) end def likes_to_destroy(people=[]) - people.each do |person| + people.first.fetch(:destroy).each do |person| define_method "destroy_#{person}!" do @current_mission = "destroy: #{person}" end @@ -27,11 +28,8 @@ def likes_to_destroy(people=[]) def good define_method :good? do - if @current_mission.include?("protect") - true - else - false - end + @current_mission.include?("protect") + end end end