Documentation as source

Are there any Ruby modules that allow documentation to act as source code? I was thinking of the problem of how souce code sometimes diverge from its documentation. So maybe there could be a way to specify the interface: the number of arguments and their types in the documentation, and possibly pre- and post-conditions, and then the documentation would be turned into code at runtime. I guess this would be implemented something like Perl's source filters[1]. Possibly some constants and such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought it through. It seems like I've read about this being done before in some other langage, but a brief search hasn't turned up anything.

Any thoughts?

Randy.

1. <http://perldoc.com/perl5.8.4/pod/perlfilter.html>

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code? I was thinking of the problem of how souce code sometimes diverge from its documentation. So maybe there could be a way to specify the interface: the number of arguments and their types in the documentation, and possibly pre- and post-conditions, and then the documentation would be turned into code at runtime. I guess this would be implemented something like Perl's source filters[1]. Possibly some constants and such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought it through. It seems like I've read about this being done before in some other langage, but a brief search hasn't turned up anything.

I haven't seen it done in Ruby, but the general idea is called "literate programming". If you google for that term, you'll find lots of information.

I'm not a big supporter of the idea, though I've never done more than tinker with it. I'd be curious to see what you come up with if you do find a way to do it in Ruby, though.

- Jamis

···

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

With duck-typing this is difficult. I wrote a method probe that tries to
figure out these method signatures, but it is incomplete because Ruby is not
100% reflective (at least not from the inside out).

Of course you're talking about putting these in documentation and generating
code. I suggest maybe going the other way around.

T.

···

On Monday 11 October 2004 10:59 pm, Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source
code? I was thinking of the problem of how souce code sometimes diverge
from its documentation. So maybe there could be a way to specify the
interface: the number of arguments and their types in the documentation,
and possibly pre- and post-conditions, and then the documentation would
be turned into code at runtime. I guess this would be implemented
something like Perl's source filters[1]. Possibly some constants and
such could be defined this way as well, version numbers, etc.

do a search for 'literate programming' - it sounds like what you are talking
about.

kind regards.

-a

···

On Tue, 12 Oct 2004, Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source
code? I was thinking of the problem of how souce code sometimes diverge
from its documentation. So maybe there could be a way to specify the
interface: the number of arguments and their types in the documentation,
and possibly pre- and post-conditions, and then the documentation would
be turned into code at runtime. I guess this would be implemented
something like Perl's source filters[1]. Possibly some constants and
such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought
it through. It seems like I've read about this being done before in some
other langage, but a brief search hasn't turned up anything.

Any thoughts?

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================

"Randy W. Sims" <ml-ruby@thepierianspring.org> schrieb im Newsbeitrag
news:416B486B.3020102@thepierianspring.org...

Are there any Ruby modules that allow documentation to act as source
code? I was thinking of the problem of how souce code sometimes diverge
from its documentation. So maybe there could be a way to specify the
interface: the number of arguments and their types in the documentation,
and possibly pre- and post-conditions, and then the documentation would
be turned into code at runtime. I guess this would be implemented
something like Perl's source filters[1]. Possibly some constants and
such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought
it through. It seems like I've read about this being done before in some
other langage, but a brief search hasn't turned up anything.

Any thoughts?

Two things come to mind:

1. literate programming (as others have mentioned already): there was a
recent thread here about the topic.

2. design by contract (DbC): there is a (experimental?) implementation of
DbC - I think by Andy Hunt.

Kind regards

    robert

···

Randy.

1. <http://perldoc.com/perl5.8.4/pod/perlfilter.html&gt;

one thing i'd like to have in ruvi is good integration
with the version control systems. this includes the ability
to mark sections of documentation (and possibly there
dependancies if they are documented) as persistantly
invalidated when code is changed. i'd like this system
to be used for testcasing also, so a dependancy tree
between the testcases and the code they test is constructed,
therefore testcases that should possibly be added to could
be highlighted. really experimental stuff. it'll be years
before i get anything working nicely i guess. but its one
of the big aims :slight_smile:

mvg,
Alex

···

On Tue, Oct 12, 2004 at 11:59:02AM +0900, Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source
code? I was thinking of the problem of how souce code sometimes diverge
from its documentation. So maybe there could be a way to specify the
interface: the number of arguments and their types in the documentation,
and possibly pre- and post-conditions, and then the documentation would
be turned into code at runtime. I guess this would be implemented
something like Perl's source filters[1]. Possibly some constants and
such could be defined this way as well, version numbers, etc.

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code?

require 'test/unit'

Then code test-first and you'll have executable documentation.

Code test-first with a partner, and you'll have /sane/ executable
documentation.

When test/unit2 appears, tests will become even more like documentation.

I've played with Literate Programming for over a decade and found
that it nearly always violates the DRY principal. As a result, it
eventually gets out of sync and begins to lie.

Regards,

···

--
Bil Kleb, Hampton, Virginia
http://fun3d.larc.nasa.gov

As other people have mentioned, what you're talking about is
essentially literate programming. It's an idea that originated with
the great Donald Knuth himself, and actually doing it actually
represents a major shift in the philosophy you have while programming.
The primary task of programming changes from explaining to the
computer what you want to do, to explaining to *people* what you want
the *computer* to do. I've been doing this kind of work using Norman
Ramsey's Noweb literate programming tool
(http://www.eecs.harvard.edu/~nr/noweb/\) and what I can say after
doing it for a while is it can be quite difficult. It forces you to be
more careful about design, as you're supposed to be able to explain
why you're doing everything, but it is a lot of work.

It does pay off in the end: look at TeX. It's one of the few
nontrivial programs that can actually make the claim of being Bug
Free(tm), without it being an overly egregious lie.

···

On Tue, 12 Oct 2004 11:59:02 +0900, Randy W. Sims <ml-ruby@thepierianspring.org> wrote:

Are there any Ruby modules that allow documentation to act as source
code? I was thinking of the problem of how souce code sometimes diverge
from its documentation. So maybe there could be a way to specify the
interface: the number of arguments and their types in the documentation,
and possibly pre- and post-conditions, and then the documentation would
be turned into code at runtime. I guess this would be implemented
something like Perl's source filters[1]. Possibly some constants and
such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought
it through. It seems like I've read about this being done before in some
other langage, but a brief search hasn't turned up anything.

Jamis Buck wrote:

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code?

I haven't seen it done in Ruby, but the general idea is called "literate programming". If you google for that term, you'll find lots of information.

If you want to try out literate programming, I recommend the "Leo" editor. It's an hierarchical text editor with features for literate programming. Leo is written in our "sister language" Python, but it isn't really tied in to any particular programming language. You can use Leo to write Ruby, C, Pascal, PHP, LaTeX, HTML or whatever you want. The web site is:

   http://webpages.charter.net/edreamleo/front.html

I've toyed a bit with literate programming, but I'm not really sold on the idea. I guess my problem with "documentation as source" (or even with "source as documentation", but that is a smaller gripe) is the notion that a program only has one "story". Usually, I find that in my programs there are many different stories: a story for a beginner user, a story for an expert user, a story for someone who wants to extend the module, a story for someone who wants to hack it, a story for someone who wants to understand and study the technology behind it. And of course, there can be stories can be in different languages. Trying to put all these stories as well as the source code in the same document tends to be confusing, at least for me.

But even if you don't use literate programming, Leo by itself is a great tool for organizing source code and text. You should check it out.

// Niklas

Jamis Buck wrote:

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code? I was thinking of the problem of how souce code sometimes diverge from its documentation. So maybe there could be a way to specify the interface: the number of arguments and their types in the documentation, and possibly pre- and post-conditions, and then the documentation would be turned into code at runtime. I guess this would be implemented something like Perl's source filters[1]. Possibly some constants and such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought it through. It seems like I've read about this being done before in some other langage, but a brief search hasn't turned up anything.

I haven't seen it done in Ruby, but the general idea is called "literate programming". If you google for that term, you'll find lots of information.

Doh! That's the term I've been trying to think of. Thanks.

I'm not a big supporter of the idea, though I've never done more than tinker with it. I'd be curious to see what you come up with if you do find a way to do it in Ruby, though.

I'm not looking for anything comprehensive. Just something to reduce the redundencies that often exist at least for the common cases. Just small stuff like (if you'll forgive a perl example, I'm more familiar with it) perl modules often declare a version:

our $VERSION = 1.03;

Then somewhere else in the POD documents, you'd have:

=head1 VERSION

   blah blah version 1.03

=cut

So anytime you change the version, you have to remember to change it in multiple places. Ideally, you could just change the docs.

This is mostly just a curiosity at the moment. I just thought I'd ask if anything like that was available that I could play with.

Randy.

Bil Kleb wrote:

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code?

require 'test/unit'

Then code test-first and you'll have executable documentation.

I've coded up test-extract which lets you embed some of your test-cases cloaked as sample code into your RDoc strings.

It looks like this in practice:

    # Creates a Regexp which matches a literal string. In this
    # string any special regular expression meta-characters will
    # be escaped automatically.
    #
    # # This creates a Regexp which will match 3 "foo"s.
    # re = Regexp::English.literal("foo" * 3)
    # re.match("foofoofoo")[0] # => "foofoofoo"
    def literal(text); Node::Literal.new(text); end

(Sample from Regexp::English)

  # Unfreeze a frozen Object. You will be able to make
  # changes to the object again.
  #
  # obj = "Hello World".freeze
  # obj.frozen? # => true
  # obj.unfreeze
  # obj.frozen? # => false
  # obj.sub!("World", "You!")
  # obj # => "Hello You!"
  def unfreeze
    if $SAFE > 0
      raise(SecurityError, "Insecure operation `unfreeze' at level #{$SAFE}")
    end

    return self if direct_value?

    self.internal.flags &= ~RubyInternal::FL_FREEZE
    return self
  end

(Sample from Evil-Ruby)

I've attached it to this email.

Code test-first with a partner, and you'll have /sane/ executable
documentation.

Heh, I have not done this yet, but I've noticed that code design generally turn out better if you do it via pair programming.

When test/unit2 appears, tests will become even more like documentation.

I'm interested: What will it change?

I've played with Literate Programming for over a decade and found
that it nearly always violates the DRY principal. As a result, it
eventually gets out of sync and begins to lie.

Would you consider the code samples from above Literate Programming?

Regards,
Bil Kleb, Hampton, Virginia

More regards,
Florian Gross

extract.rb (2.76 KB)

Bil Kleb <Bil.Kleb@NASA.Gov> writes:

Randy W. Sims wrote:
> Are there any Ruby modules that allow documentation to act as source
> code?

require 'test/unit'

Then code test-first and you'll have executable documentation.

Code test-first with a partner, and you'll have /sane/ executable
documentation.

When test/unit2 appears, tests will become even more like documentation.

What's in the works for test/unit2?

Randy W. Sims wrote:

Jamis Buck wrote:

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code? I was thinking of the problem of how souce code sometimes diverge from its documentation. So maybe there could be a way to specify the interface: the number of arguments and their types in the documentation, and possibly pre- and post-conditions, and then the documentation would be turned into code at runtime. I guess this would be implemented something like Perl's source filters[1]. Possibly some constants and such could be defined this way as well, version numbers, etc.

I'm not sure if this is a good idea or not; I haven't completely thought it through. It seems like I've read about this being done before in some other langage, but a brief search hasn't turned up anything.

I haven't seen it done in Ruby, but the general idea is called "literate programming". If you google for that term, you'll find lots of information.

Doh! That's the term I've been trying to think of. Thanks.

I'm not a big supporter of the idea, though I've never done more than tinker with it. I'd be curious to see what you come up with if you do find a way to do it in Ruby, though.

I'm not looking for anything comprehensive. Just something to reduce the redundencies that often exist at least for the common cases. Just small stuff like (if you'll forgive a perl example, I'm more familiar with it) perl modules often declare a version:

our $VERSION = 1.03;

Then somewhere else in the POD documents, you'd have:

=head1 VERSION

  blah blah version 1.03

=cut

So anytime you change the version, you have to remember to change it in multiple places. Ideally, you could just change the docs.

This is mostly just a curiosity at the moment. I just thought I'd ask if anything like that was available that I could play with.

Randy.

Only for this special case, you could use
# blah blah blah
# This is
VERSION = '1.03'
# blah blah blah.

Which is quite readable and valid ruby code.

regards

Brian

···

--
Brian Schröder
http://ruby.brian-schroeder.de/

Randy W. Sims ha scritto:

This is mostly just a curiosity at the moment. I just thought I'd ask if anything like that was available that I could play with.

I'd check if RDoc can solve your problems.. then patch it to do it if it does'nt :slight_smile:

*smile* Wonderfully put.

    -- Markus

···

On Tue, 2004-10-12 at 00:44, Niklas Frykholm wrote:

I've toyed a bit with literate programming, but I'm not really sold on
the idea. I guess my problem with "documentation as source" (or even
with "source as documentation", but that is a smaller gripe) is the
notion that a program only has one "story". Usually, I find that in my
programs there are many different stories: a story for a beginner user,
a story for an expert user, a story for someone who wants to extend the
module, a story for someone who wants to hack it, a story for someone
who wants to understand and study the technology behind it. And of
course, there can be stories can be in different languages. Trying to
put all these stories as well as the source code in the same document
tends to be confusing, at least for me.

Well, the current plan is to let you to do something like this:

   class Adder
     include Testing

     # Adds two numbers
     example { assert_equal(2, Adder.new.add(1, 1)) }
     def add(a, b)
       a + b
     end
   end

Examples will then be collected and run as tests when you're in testing mode, and (eventually) RDoc will understand the example blocks, so we can get output like this:

   add(a, b)

   Adds two numbers

     Adder.new.add(1, 1) => 2

I haven't yet had a chance to look through your test extractor (though I was encouraged to pursue this idea by it), but from a cursory survey it seems to take quite a different approach. Can you compare the approaches?

HTH,

Nathaniel
Terralien, Inc.

<:((><

···

On Oct 12, 2004, at 07:39, Florian Gross wrote:

When test/unit2 appears, tests will become even more like documentation.

I'm interested: What will it change?

Quite a bit... for a preview, you can see my RubyConf slides here:

   http://terralien.biz/RubyConf2004.tar.gz

Formats include Keynote (the original), Powerpoint, Quicktime and PDF.

Let me know if you have any more questions,

Nathaniel
Terralien, Inc.

<:((><

···

On Oct 12, 2004, at 07:42, Mikael Brockman wrote:

What's in the works for test/unit2?

Florian Gross wrote:

Bil Kleb wrote:

Randy W. Sims wrote:

Are there any Ruby modules that allow documentation to act as source code?

require 'test/unit'

Then code test-first and you'll have executable documentation.

I've coded up test-extract which lets you embed some of your test-cases cloaked as sample code into your RDoc strings.

It looks like this in practice:

    # Creates a Regexp which matches a literal string. In this
    # string any special regular expression meta-characters will
    # be escaped automatically.
    #
    # # This creates a Regexp which will match 3 "foo"s.
    # re = Regexp::English.literal("foo" * 3)
    # re.match("foofoofoo")[0] # => "foofoofoo"
    def literal(text); Node::Literal.new(text); end

(Sample from Regexp::English)

I very much like the idea of being able to test examples embedded in the source file's documentation. A couple of suggestions:

1. Is there some way to allow set-up code that is not part of the viewable documentation? This would make for clearer examples. Eg. the above might be changed to something like:

···

#--
   # Init:
   # foo = Foo.new(blah, blah);
   #++
   #
   # An example of using Foo.bar()
   #
   # foo.bar(blek)

   def bar(); ...

Well, that's not real pretty, but... the idea is that the example is more focused for documentation purposes, and all the setup-code is hidden away.

2. In addition to showing how something works, it is sometimes instructive to show what doesn't work. Being able to create examples that are expected to fail would be useful.

Randy.

Nathaniel Talbott wrote:

When test/unit2 appears, tests will become even more like documentation.

I'm interested: What will it change?

Well, the current plan is to let you to do something like this:

  class Adder
    include Testing

    # Adds two numbers
    example { assert_equal(2, Adder.new.add(1, 1)) }
    def add(a, b)
      a + b
    end
  end

Examples will then be collected and run as tests when you're in testing mode, and (eventually) RDoc will understand the example blocks, so we can get output like this:

Ah, that looks very nifty. :slight_smile:

  add(a, b)

  Adds two numbers

    Adder.new.add(1, 1) => 2

I haven't yet had a chance to look through your test extractor (though I was encouraged to pursue this idea by it), but from a cursory survey it seems to take quite a different approach. Can you compare the approaches?

Mine takes the RDoc style samples and converts it to a test-case and the above mentioned takes a short test case and converts it to sample code.

I think we're doing the same, but from opposite ends.

HTH,
Nathaniel
Terralien, Inc.

Regards,
Florian Gross

···

On Oct 12, 2004, at 07:39, Florian Gross wrote:

Markus wrote:

···

On Tue, 2004-10-12 at 00:44, Niklas Frykholm wrote:

I've toyed a bit with literate programming, but I'm not really sold on the idea. I guess my problem with "documentation as source" (or even with "source as documentation", but that is a smaller gripe) is the notion that a program only has one "story". Usually, I find that in my programs there are many different stories: a story for a beginner user, a story for an expert user, a story for someone who wants to extend the module, a story for someone who wants to hack it, a story for someone who wants to understand and study the technology behind it. And of course, there can be stories can be in different languages. Trying to put all these stories as well as the source code in the same document tends to be confusing, at least for me.

     *smile* Wonderfully put.

Where do all of these different stories go, if not together, such that when the code changes all of the stories are updated as well?

James