-
-
Save deepak/149877 to your computer and use it in GitHub Desktop.
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
# Playing with it in irb: | |
>> (class << Widget; self; end).ancestors | |
=> [BazWrapper, BarWrapper, FooWrapper, Class, Module, Object, Kernel] | |
>> widget = Widget.new | |
=> #<Widget:0x11771f8> | |
>> (class << widget; self; end).ancestors | |
=> [BazWrapper::WrappingB, BazWrapper::WrappingA, BarWrapper::Wrapping, FooWrapper::Wrapping, | |
Widget, Object, Kernel] | |
>> widget.render_on(nil) | |
BazWrapper::WrappingB | |
BazWrapper::WrappingA | |
BarWrapper::Wrapping | |
FooWrapper::Wrapping | |
Original | |
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
class Widget | |
def render_on(document, options = {}) | |
puts "Original" | |
end | |
end | |
module FooWrapper | |
def new(*args, &block) | |
super.extend Wrapping | |
end | |
module Wrapping | |
def render_on(*args) | |
puts "FooWrapper::Wrapping" | |
super | |
end | |
end | |
end | |
module BarWrapper | |
def new(*args, &block) | |
super.extend Wrapping | |
end | |
module Wrapping | |
def render_on(*args) | |
puts "BarWrapper::Wrapping" | |
super | |
end | |
end | |
end | |
module BazWrapper | |
def new(*args, &block) | |
instance = super | |
instance.extend(WrappingA) | |
instance.extend(WrappingB) | |
end | |
module WrappingA | |
def render_on(*args) | |
puts "BazWrapper::WrappingA" | |
super | |
end | |
end | |
module WrappingB | |
def render_on(*args) | |
puts "BazWrapper::WrappingB" | |
super | |
end | |
end | |
end | |
Widget.extend FooWrapper | |
Widget.extend BarWrapper | |
Widget.extend BazWrapper | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment