Last active
December 10, 2015 04:08
-
-
Save jlebrech/4379243 to your computer and use it in GitHub Desktop.
Dynamic Method Module
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
class MyClass | |
def initialiser | |
defn /do_([a-z]*)_to_([a-z]*)/, :do_x_to_y | |
end | |
def defn(regex, &proc) | |
@@missing_things = [] if @@missing_things.nil? | |
@@missing_things << [ regex, proc ] | |
end | |
def method_missing(name, *args, &block) | |
@@missing_things.each do |method| | |
name.match(method[0]) do |matches| | |
return method[1].call(args.merge(matches), block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment