Skip to content

Instantly share code, notes, and snippets.

@jlebrech
Last active December 10, 2015 04:08
Show Gist options
  • Save jlebrech/4379243 to your computer and use it in GitHub Desktop.
Save jlebrech/4379243 to your computer and use it in GitHub Desktop.
Dynamic Method Module
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