Skip to content

Instantly share code, notes, and snippets.

@h-lame
Created November 17, 2009 14:49
Show Gist options
  • Save h-lame/236945 to your computer and use it in GitHub Desktop.
Save h-lame/236945 to your computer and use it in GitHub Desktop.
class Module
def private(*args)
if args.empty?
@acl = :private
else
args.each do |meth|
alias_method :"__#{meth}", meth
undef_method meth
end
end
end
alias_method :orig_public, :public
def public(*args)
if args.empty?
@acl = :public
end
orig_public
end
alias_method :orig_method_added, :method_added if self.instance_methods.include?(:method_added)
def method_added(meth)
return if meth.to_s =~ /^__/
orig_method_added(meth) if self.respond_to?(:orig_method_added)
if @acl && @acl == :private
alias_method :"__#{meth}", meth
public :"__#{meth}"
undef_method meth
end
end
end
class N
def zoo
:zoo
end
def poo
:poo
end
private :poo
public
def choo
:choo
end
private
def woo
:woo
end
public
def moo
:moo
end
end
n = N.new
n.poo
n.__poo
n.woo
n.__woo
n.zoo
n.choo
n.moo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment