Loading plugins with Rubygems

Let’s say you have a Rubygem named “blorf”. You want to enable other developers to write plugins in the forms of Rubygems of their own. For the end user, loading the plugins should be as simple as writing:

In order to make this work, blorf needs to look for files matching a certain path pattern inside other loaded gems. If a gem contains a file matching ‘lib/blorf/plugins/**/*/rb’, it should be loaded when blorf is loaded.

Rubygems provides a method which almost fits the bill. The only problem with Gem#find_files is that it will find files matching that pattern in all installed gems–not just in the gems that have been loaded. If you have multiple versions of a Gem installed it will find paths in all the installed versions. We want the end user to control gem versions, so this method doesn’t help us.

Here’s some code which will look in only loaded gems for files matching a given globbing pattern. It’s complicated by the fact that the Rubygems API for this changed (for the better) at version 1.8.0. This code has been tried on Rubygems versions 1.8.7 and 1.6.2.

Yes, this is messy first-working-version exploratory code. It could stand to be cleaned up.

At Eric Hodel’s encouragement I’ve submitted a ticket which, if accepted, would reduce this to a one-liner.