Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created February 15, 2012 20:48
Show Gist options
  • Save brainopia/1838899 to your computer and use it in GitHub Desktop.
Save brainopia/1838899 to your computer and use it in GitHub Desktop.
# "python-like" import
load path, true # => will load new constants and methods under anonymous module
Module.nesting.first # => inside loaded file will return the current namespace
###
module PythonImport
def import(path)
load 'importer.rb', true
Thread.current[:imported_module]
end
def can_be_imported
Thread.current[:imported_module] = Module.nesting.first
end
end
extend PythonImport
# file1
require 'python_import'
import('file2')::Foo.bar # => I'm invisible
Foo.bar # => uninitialized constant Boo
# file2
can_be_imported
module Foo
extend self
def bar
puts "I'm invisible"
end
end
# right way is to use include/extend like the Matz intended
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment