Module: Kernel
- Defined in:
- lib/nakajima/likewise.rb,
lib/nakajima/core_ext/kernel.rb
Overview
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
Instance Method Summary collapse
Instance Method Details
#blank_context(*args, &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/nakajima/core_ext/kernel.rb', line 2 def blank_context(*args, &block) ivars = args. args.push(/^__/) klass = Class.new do instance_methods.each do |m| undef_method(m) unless args.any? { |pattern| m =~ pattern } end end klass.class_eval(&block) if block_given? instance = klass.new setter = Object.instance_method(:instance_variable_set).bind(instance) ivars.each { |key, value| setter.call("@#{key}", value) } instance end |
#likewise(&blk) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nakajima/likewise.rb', line 14 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 |