Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active May 1, 2016 05:23
Show Gist options
  • Save benolee/6e7a4c7184e5f76558b8 to your computer and use it in GitHub Desktop.
Save benolee/6e7a4c7184e5f76558b8 to your computer and use it in GitHub Desktop.
def f a, b = Proc.new
Module.new {
const_set :M__, self
module_exec self, *a, &b
}
end
m1 = module f(:m1_ok) { |mod, sym| @foo = sym }::M__
def foo
@foo
end
extend self
self
end
p m1.foo #=> :m1_ok
m2 = module f(:m2_ok, -> mod, sym {
extend m1
@bar = sym
})::M__
@foo = :m2_foo
BAR = @bar
self
end
p m2.foo #=> :m2_foo
p m2::BAR #=> :m2_ok
m3 = module f(:m3_ok) do |mod, sym|
extend m1
self::FOO = sym
end::M__
@foo = [FOO, Module.nesting]
self
end
p m3.foo #=> [:m3_ok, [#<Module:0x41398b90>]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment