2014-03-19 :-(
_ 午後
1300 デバッグしTARI
_ [ruby]呼び出す関数を文字列に結びつける
文字列をキーにして処理を呼び出すだけですが。
やり方はいろいろあるだろうけどハッシュにしてしまうのが最も簡単かと。
#: coding: utf-8
class Command
def initialize()
@command ||= {}
end
def register(name, cmd)
@command[name] = cmd
end
def exec(name)
@command[name]
end
end
def func0()
puts "func0"
end
def func1()
puts "func1"
end
def func2()
puts "func2"
end
def main(argv)
command = Command.new
command.register("a", func0)
command.register("b", func1)
command.register("c", func2)
command.exec("a")
command.exec("b")
command.exec("c")
end
main(ARGV)
>ruby command0.rb func0 func1 func2
[ツッコミを入れる]




