-
-
Save nakajima/111588 to your computer and use it in GitHub Desktop.
Ruby's fun, right?
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
# likewise require | |
# by Pat Nakajima | |
# because I've been drinking more than Jack Barnette | |
# and | |
# to annoy John Susser and Aaron Peterson | |
# | |
# use to require files relative to the current file: | |
# | |
# likewise do | |
# require "foo" # require current-dir/lib | |
# require "lib/foo" # require current-dir/lib/foo | |
# end | |
module Kernel | |
def likewise(&blk) | |
Class.new { | |
instance_methods.each { |m| undef_method(m) unless m.to_s =~ /__/ } | |
def initialize(&block) | |
@outside = eval('self', block.binding) | |
Object.instance_method(:instance_eval).bind(self).call(&block) | |
end | |
def require(path) | |
super(File.join(File.dirname(caller.first.split(':').first), path)) | |
end | |
def method_missing(sym, *args, &block) | |
@outside.send(sym, *args, &block) | |
end | |
}.new(&blk) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment