How to create your own plugins for your Ruby programs

In the post How to add your own scripts to Ruby loader path and solve load errors I solved a very annoying problem for newbies (like me).

With that issue addressed I continued to develop my project. It basically is composed by a main file that loads all scripts from lib sub-folder and executes some methods.

If you have just 2 or 3 files to load, it’s easier to open the main file and update it to add the require ‘script_name’, instantiation of the class and executes the desired method.

When the number of scripts starts to rising it no only gets annoying to have to change main file and add all required commands for each script but it increases the size of main file too.

And this process is error prone. Definitely it’s not classy. I want instantiate and run all files automatically.

So I looked for a way to solve this problem. Guess what? I found out.

The problems has 2 parts:

– define a common interface for scripts.

– automatically load and run all of scripts.

To solve this problem I’ll use classes. The idea is: a base class will implement all methods that should be called. In really, I won’t implement them, I’ll just define empty methods.

In other words, I’m doing plugins / extensions to my program.

For each subclass, I override only the method that it needs to implement.

See bellow Base class, MyPlugin class and MyGreatPlugin class.

Listing 1 – base.rb

listing 2 – my_plugin.rb

listing 3 – my_great_plugin.rb

OK. Problem partially solved. I already have the interfaces defined. What is missing? A way to automatically load all plugins  / extensions without our intervention.

To accomplish that, we will use a method named  inherited. This method is invoked each time a subclass of current class is created.

The idea is to create a list of subclasses that were instantiated.

Following you can see the change into to Base class code:

Listing 4 – Final version of Base class

Last, but no least, we insert the required calls to our main script file (see listing 5)

Listing 5 – Main script. Its responsible to load and run every plugin it founds in lib sbu-folder

The structure is the same from the How to add your own scripts to Ruby loader path and solve load errors.:

The great thing about this technique is that as long you don’t need to add new methods but just new implementations to the existing ones you don’t need to touch main file anymore.

For each new plugin, just make it a subclass of Base class implementing the methods you want and put the file into lib folder. It will be automagically loaded and run.

Oh, what if you need to add a method that does not exist in Base class?

In this case you have more work to do:

  • add the method definition in the base class.
  • implement the new plugin
  • change main file to add call to the new method.

You can download the project here.

Once you’ve downloaded the project , why don’t you create a new plugin to see how it behaves ?


Please, try my games, play free on-line games on my site, tweet this post url and share it on facebook, google+ and other social medias.

This entry was posted in development and tagged , , . Bookmark the permalink.