Created
June 24, 2009 15:01
-
-
Save trans/135317 to your computer and use it in GitHub Desktop.
Module Clone Methods
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
# Module#clone_xxx methods. These methods make it easy to | |
# create new subsets (or sidesets) of current modules. | |
# However facets/module pseudo-traits methods are better. | |
class Module | |
# Returns an anonymous module with the specified methods | |
# of the receiving module renamed. | |
def clone_renaming( pairs ) | |
mod = self.dup | |
pairs.each_pair do |to_sym, from_sym| | |
mod.class_eval { | |
alias_method( to_sym, from_sym ) | |
undef_method( from_sym ) | |
} | |
end | |
return mod | |
end | |
# Returns an anonymous module with the specified methods | |
# of the receiving module removed. | |
def clone_removing( *meths ) | |
mod = self.dup | |
methods_to_remove = meths | |
methods_to_remove.each{ |m| mod.class_eval{ remove_method m } } | |
return mod | |
end | |
# Returns an anonymous module with only the specified methods | |
# of the receiving module intact. | |
def clone_using( *methods ) | |
methods.collect!{ |m| m.to_s } | |
methods_to_remove = instance_methods - methods | |
mod = self.dup | |
mod.class_eval{ methods_to_remove.each{ |m| undef_method m } } | |
return mod | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment