Skip to content

Instantly share code, notes, and snippets.

@ajbeach2
Created June 23, 2015 18:38
Show Gist options
  • Save ajbeach2/d710b48754053191f726 to your computer and use it in GitHub Desktop.
Save ajbeach2/d710b48754053191f726 to your computer and use it in GitHub Desktop.
module Runnable
def run(command, *args)
fail "#{command} is not a valid action!" unless actions.include? command
send(command, *args)
end
def runnables(*args)
define_singleton_method(:actions) do
args.map(&:to_s)
end
end
end
class MyObj
extend Runnable
runnables :hello
def self.hello(name)
puts "hello #{name}"
end
end
puts MyObj.actions # ["hello"]
puts MyObj.run("hello", "Bob") #hello bob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment