# Help design a plugin system for a DSL

**URL:** <https://www.ruby-forum.com/t/help-design-a-plugin-system-for-a-dsl/113296>\
**Category:** Ruby\
**Created:** [August 29, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/help-design-a-plugin-system-for-a-dsl/113296 "2007-08-29T17:06:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [August 29, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/help-design-a-plugin-system-for-a-dsl/113296/1 "2007-08-29T17:06:57Z")

</div>

Hello,

I’ve been working through the “Creating DSLs with Ruby” tutorial here :  
[http://www.artima.com/rubycs/articles/ruby\_as\_dsl4.html](http://www.artima.com/rubycs/articles/ruby_as_dsl4.html)

In my DSL I’d like users to be able to write code such as this :

myDSL do  
plugin “extra\_functions”

a\_special\_function  
basic\_function  
end

that is, to extend the range of callable functions using a kind of  
“plugin” system.

The code that I have to implement this so far looks like :

class myDSL  
def initialize(name)  
@name = name  
end

def self.load(name)  
dsl = new(name)  
dsl = dsl.instance\_eval(File.read(name))  
dsl  
end

def myDSL  
yield  
end

def plugin(file)  
puts “loading plugin: #{file}”  
require “./plugins/#{file}.rb”  
end

def basic\_function()  
# a “core” method of the DSL.  
end  
end

which is called using a short loader file like this :

require ‘myDSL’  
name = ARGV.shift  
name = myDSL.load(name)

In the extra\_functions.rb “plugin” I have a method definition

def a\_special\_function

# does something special

end

I’m concerned that this is poor design, as the plugin functions will not  
be very well encapsulated. Requiring two plugins that define the same  
method names will cause the wrong methods to be called from the DSL.

What is a sensible way to solve this problem ? My thought is to use  
method\_missing to traverse plugin Classes in the order they were loaded,  
looking for the function called. Is this a good way to proceed ? Any  
other suggestions ?

Chris

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [August 29, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/help-design-a-plugin-system-for-a-dsl/113296/2 "2007-08-29T23:13:29Z")

</div>

With a dynamic language as flexible as Ruby, using a ‘plug-in’  
metaphor/technique/description feels to me like the 4GL languages of  
ten.ish years ago.

I cannot claim to understand everything.  
Though here the writing between the lines was that understanding is  
optional.

Plug-ins are great for Firefox. Where the end-user is the end-user.

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [August 29, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/help-design-a-plugin-system-for-a-dsl/113296/3 "2007-08-29T23:21:02Z")

</div>

> Though here the writing between the lines was that understanding is  
> optional.

Thank you for your comments. Apologies if my explanation was unclear.  
The end result I’d like is to allow the user to issue a command in the  
DSL that unlocks for them additional commands to use in the DSL.

To give a concrete example, this is an idea for a hardware automation  
system (for automating experiments). The user can type “plugin  
Acquisition System A” and access a series of commands for controlling  
acquisition system A. There may be other “plugins” for stepper motors  
for example.

Apologies if the terminology “plugin” is antiquated, or wrong - I was  
trying to choose a term that my users (and I!) can grasp!

Would appreciate any more thoughts on the best way to achieve this,

Chris
