Skip to content

Instantly share code, notes, and snippets.

@nakajima
Forked from jbarnette/adjacent.rb
Created May 14, 2009 09:41
Show Gist options
  • Save nakajima/111588 to your computer and use it in GitHub Desktop.
Save nakajima/111588 to your computer and use it in GitHub Desktop.
Ruby's fun, right?
# 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