Literate Ruby

This library is the first step towards a more literate Ruby syntax, similar to Haskell’s (admittedly semi-literate syntax).

Applications include: documentation files where there’s more text than code (e.g. like READMEs!), blog entries, or a minimalist story runnner.

Usage

Single lines code


> puts "This is inline code."
> puts "But this will be part of the same code block."

Block code


=begin_code
def describe_block_code
  puts "This will all be part of the same code block."
end

describe_block_code
=end_code

Loading


=begin_code
require 'pp'
require './lib/literate_ruby'

lines = LiterateRuby.load("test/fixtures/fixture.lrb")

pp lines

pp "FOO: #{FOO}"
pp "foo (method): #{foo()}"
=end_code

Parsing

If you just want access to the raw tree without eval’ing the code, do this:


=begin_code
require 'pp'
require './lib/literate_ruby'

lines = LiterateRuby.parse(File.new("test/fixtures/fixture.lrb"))

pp lines

=end_code

This file is like, so self-referential, or whatever

Aim your shells thusly for great justice:


$ ruby -r./lib/literate_ruby -e "LiterateRuby.load('README.textile')"

(from this directory)

TODOS

  • Figure out if we need/how best to finish off the LiterateRuby.require method. So someone could write library code if they so desired.
  • Possibly change or make the inline & block delimiters configurable to support different markdown languages’ code syntax.