Created
August 19, 2009 17:18
-
-
Save charly/170482 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
require 'rubygems' | |
require 'facets' | |
module Creme | |
def cost | |
puts "before_filter" | |
super | |
end | |
end | |
class Coffee | |
prepend Creme | |
def cost | |
puts "3" | |
end | |
end | |
coffee = Coffee.new | |
coffee.cost | |
#But guess what, it's by overriding new: | |
# facet library | |
def prepend( aspect ) | |
_new = method(:new) | |
_allocate = method(:allocate) | |
(class << self; self; end).class_eval do | |
define_method(:new) do |*args| | |
o = _new.call(*args) | |
o.extend aspect | |
o | |
end | |
define_method(:allocate) do |*args| | |
o = _allocate.call(*args) | |
o.extend aspect | |
o | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment