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
# Subject: f.rb | |
# From: Oleg Andreev <oleganza@gmail.com> | |
# Date: November 9, 2008 | |
module Kernel | |
# Private functions for mixins (to avoid private methods clash) | |
# Usage: | |
# module MyModule | |
# def my_api_method | |
# F.some_private_function() | |
# end | |
# | |
# F do | |
# def some_private_function | |
# end | |
# end | |
# end | |
def F(&blk) | |
m = Module.new | |
remove_const(:F) if const_defined?(:F) | |
const_set(:F, m) | |
m.module_eval(&blk) | |
m.extend(m) | |
m | |
end | |
end | |
if $0 == __FILE__ | |
module MyModule | |
def some_method | |
F.private_function | |
end | |
F do | |
def private_function | |
self | |
end | |
end | |
end | |
obj = Object.new | |
obj.extend(MyModule) | |
(obj.some_method == MyModule::F) or raise "Test failed!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment