-
-
Save rkh/2720016 to your computer and use it in GitHub Desktop.
Revisions
-
rkh revised this gist
May 17, 2012 . 1 changed file with 11 additions and 2 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -14,11 +14,20 @@ def call end end module Prepending def append_features(base) return super unless base.is_a? Class prepend = self base.extend Module.new { define_method(:new) { |*| super.extend(prepend) }} end end p Base.new.call #=> "monkeypatched" # This is the spirit of what I'd like, but methods defined on the class will be # preferred over those in ancestors. module Override extend Prepending def call [ 'overridden', super ] end @@ -28,7 +37,7 @@ class Base include Override end p Base.new.call #=> ["overridden", "monkeypatched"] @@ -39,4 +48,4 @@ def call [ 'overridden', super ] end end p instance.call #=> ["overridden", ["overridden", "monkeypatched"]] -
lmarburger created this gist
May 17, 2012 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ class Base def call 'call' end end p Base.new.call #=> "call" # Monkeypatching "works" but doesn't provide access to #super class Base def call 'monkeypatched' end end p Base.new.call #=> "monkeypatched" # This is the spirit of what I'd like, but methods defined on the class will be # preferred over those in ancestors. module Override def call [ 'overridden', super ] end end class Base include Override end p Base.new.call #=> "monkeypatched" # This works but I don't have access to the class instances to apply this method. instance = Base.new class << instance def call [ 'overridden', super ] end end p instance.call #=> ["overridden", "monkeypatched"]