Created
April 14, 2011 05:18
-
-
Save janxious/918932 to your computer and use it in GitHub Desktop.
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
Any of these should be runnable at the command line by `ruby <filename>`. |
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
# check this out: http://www.nach-vorne.de/2007/3/18/list-of-callback-methods/ | |
module M | |
def self.included(base) | |
puts "#{base.inspect} just included me!" | |
end | |
def self.extended(base) | |
puts "#{base.inspect} just got extended!" | |
end | |
end | |
class C | |
include M | |
end | |
class D | |
extend M | |
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
class C | |
end | |
module M | |
def hello | |
"I am from M" | |
end | |
end | |
module N | |
def hello | |
"I am from N" | |
end | |
end | |
C.send :include, M | |
puts C.new.hello | |
class C | |
include N | |
end | |
puts C.new.hello |
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
module M | |
def hello | |
"I am from M" | |
end | |
end | |
module N | |
def hello | |
"I am from N" | |
end | |
end | |
class C | |
include M | |
alias :original_hello :hello | |
include N | |
end | |
puts C.new.methods.sort.inspect | |
puts C.new.respond_to? :original_hello | |
puts C.new.original_hello | |
puts C.new.hello |
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
class C | |
def hello | |
"Hello from C" | |
end | |
end | |
module M | |
def hello | |
"Hello from M" | |
end | |
end | |
puts C.new.hello | |
C.send :include, M | |
puts C.new.hello | |
C.class_eval do | |
alias :old_hello :hello | |
include M | |
end | |
puts C.new.hello | |
C.class_eval do | |
def hello | |
"have some h8" | |
end | |
end | |
puts C.new.hello |
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
# init.rb | |
ActiveRecord::Base.send :include, ExpectedBehavior::ActsAsArchivalActiveRecordMethods | |
# acts_as_archival_active_record_methods.rb | |
module ExpectedBehavior | |
module ActsAsArchivalActiveRecordMethods | |
def self.included(base) | |
base.extend ARClassMethods | |
base.send :include, ARInstanceMethods | |
end | |
module ARClassMethods | |
def is_archival? | |
self.included_modules.map(&:to_s).include?("ExpectedBehavior::ActsAsArchival::InstanceMethods") | |
end | |
end | |
module ARInstanceMethods | |
def is_archival? | |
self.class.is_archival? | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment