Created
April 26, 2017 02:48
-
-
Save lucianspec/4fc0e47453a04815c140845850be5aea to your computer and use it in GitHub Desktop.
Ruby instance_method
This file contains hidden or 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 Interpreter | |
def do_a() print "there, "; end | |
def do_d() print "Hello "; end | |
def do_e() print "!\n"; end | |
def do_v() print "Dave"; end | |
Dispatcher = { | |
"a" => instance_method(:do_a), | |
"d" => instance_method(:do_d), | |
"e" => instance_method(:do_e), | |
"v" => instance_method(:do_v) | |
} | |
def interpret(string) | |
string.each_char {|b| Dispatcher[b].bind(self).call } | |
end | |
end | |
interpreter = Interpreter.new | |
interpreter.interpret('dave') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment