ruby、pythonでプラグイン的な感じに動的にモジュールを読み込むめも。
どちらもモジュールを読み込むファイルとモジュールのファイルは同じパスにあるという前提です。
まずはpythonから
モジュールを読み込んで使うファイル。
#!/usr/bin/env python import sys import imp def load_plugins(): try: (file, pathname, description) = imp.find_module("hello_plugin") plugin = imp.load_module("hello_plugin", file, pathname, description) plugin.say_hello() except ImportError: print "Failed to load module" if __name__ == "__main__": load_plugins()
こちらが読み込まれる側。
def say_hello(): print "Hello, World!"
次にruby。
こちらもモジュールを読み込んで使う側から。
#!/usr/bin/env ruby def load_plugin() f = sprintf "%s/HelloPlugin.rb" ,File.expand_path(File.dirname(__FILE__)) require f include HelloPlugin say_hello() end if __FILE__ == $0 load_plugin() end
読み込まれる側。
module HelloPlugin def say_hello() print "Hello, World!\n" end end