Skip to content

Instantly share code, notes, and snippets.

@adelcambre
Last active May 17, 2017 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelcambre/4c4b3c883f5098b1c2e996564cd13f23 to your computer and use it in GitHub Desktop.
Save adelcambre/4c4b3c883f5098b1c2e996564cd13f23 to your computer and use it in GitHub Desktop.
Module.new do
def prepend(m=nil, &blk)
if block_given?
super(Module.new(&blk))
else
super
end
end
end.tap { |pre| Module.prepend pre }
class Foo
def bar
puts "class"
end
end
Foo.prepend do
def bar
puts "prepended"
super
end
end
module Bar
def bar
puts "module"
super
end
end
Foo.prepend Bar
Foo.new.bar # => module
# prepended
# class
@naaman
Copy link

naaman commented May 16, 2017

To the poor person that googled their way to this, please don't do this.

@rwz
Copy link

rwz commented May 16, 2017

There's no need to explicitly pass arguments to super on line 6. Just calling super with no parens passes all original arguments by default because Ruby is all about consistency.

Edit: Sorry, I meant line 6.

@adelcambre
Copy link
Author

@rwz Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment