polyglot

Get Version

0.3.1

Poly => many, glot => languages

What

Polyglot provides a registry of file types that can be loaded by
calling its improved version of ‘require’. Each file extension
that can be handled by a custom loader is registered by calling
Polyglot.register(“ext”, <class>), and then you can simply
require “somefile”, which will find and load “somefile.ext”
using your custom loader.

This supports the creation of DSLs having a syntax that is most
appropriate to their purpose, instead of abusing the Ruby syntax.

Required files are attempted first using the normal Ruby loader,
and if that fails, Polyglot conducts a search for a file having
a supported extension.

Installing

sudo gem install polyglot

Example

Define and register your file type loader in file rubyglot.rb:

    require 'polyglot'
    class RubyglotLoader
        def self.load(filename, options = nil, &block)
            File.open(filename) {|file|
                # Load the contents of file as Ruby code:
                # Implement your parser here instead!
                Kernel.eval(file.read)
            }
        end
    end
    Polyglot.register("rgl", RubyglotLoader)

This file, hello.rgl, will be loaded (this simple example uses Ruby code):

    puts "Initializing"
    class Hello
        def initialize()
            puts "Hello, world\n"
        end
    end

Call it from file test.rb:

    require 'rubyglot'	# Create my file type handler
    require 'hello'	# Can add extra options or even a block here
    puts "Ready to go"
    Hello.new

Run:

    $ ruby test.rb
    Initializing
    Ready to go
    Hello, world
    $

How to submit patches

Read the 8 steps for fixing other people’s code and for section 8z: Submit patch, email me on the link below.

The trunk repository is http://github.com/cjheath/polyglot.

License

This code is free to use under the terms of the MIT license.

Contact

Comments are welcome. Send an email to Clifford Heath

Clifford Heath, 15th June 2010
Theme extended from Paul Battley