class Module
# Note: view is public, private, protected, or nil.
def def_methods(view=nil, &block)
@defmethods ||= DefMethods.new(self, view)
@defmethods.instance_eval(&block)
end
class DefMethods
private *instance_methods.select{ |m| m !~ /^(__|instance_eval)/ }
def initialize(base, view=nil)
@base = base
@view = view
end
def method_missing(s, *a, &b)
@base.__send__(:define_method, s, &b)
@base.__send__(@view, s) if @view
end
end
end
# Example
class X
def_methods(:public) do
world = "World!"
hello do
"Hello"
end
hello_world do
hello + ' ' + world
end
end
end
x = X.new
puts x.hello_world