[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Literate Ruby (#102

Ross Bamford

11/19/2006 8:20:00 PM

(Apologies if this turns out to be a dupe - I posted this about an hour
ago on the newsgroup and it doesn't seem to be showing up anywhere).

Here's my solution to this quiz. It's basically a non-too-efficient
regex-based solution wrapped in a combination command-line script and
library. The main file (lrb.rb) contains both the library and script
code, while the other three files make up some tests of the
functionality in lrb.rb.

To run the tests, use the lrb script:

$ ./lrb.rb test.lrb

If you like, lrb will output the code it's about to run before running
it if you enable ruby debugging:

$ ./lrb.rb test.lrb -d

The first argument has to be the filename. If the second argument is a
number, it's taken as the index of the code block to run. Following
arguments are passed to the ruby interpreter that replaces the one
running lrb. You can run individual tests using this, but you need to
manually require test/unit. E.g:

$ ./lrb.rb test.lrb 1 -rtest/unit
$ ./lrb.rb test.lrb 2 -rtest/unit

(the numbers are 0-based, but the tests have a 'require test/unit' line
at block 0 which does nothing on it's own). If you need to pass a number
as the first argument to your script, give the block number as -- .

You can also get a dump of the extracted code, rather than running it:

$ ./lrb.rb test.lrb --dump

When used as a library, lrb provides a literate version of eval
(lrb_eval) and an lrb-enabled require (lrb_require) that won't work with
rubygems :(. The library is not auto-required into the interpreter that
runs the lrb code.

It has it's warts (especially, no handling of piped-in code or data, no
nested \begin...\end blocks, and debugging can be a PITA) but generally
it seems to work and is pretty flexible.

Thanks for this quiz - literate programming is something I'm still
finding out about (mostly via Haskell, which I've just started looking
at properly) so this was an interesting quiz topic for me :)

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk