viernes, 18 de marzo de 2016

programming quizzes with org-babel

When trying to write some proof of concept, or an algorithm in isolation (or programming quizes), I usually create a new file (foo.lua, for example), and run it with my patched version of shell-execute that replaces '%' by the current file name .

But sometimes I want to give more context or write the code as a story, or a question-answer dialogue.  In those occasions, org-babel is the way to go.
This piece of elisp enables a bunch of languages to be evaluated in org file blocks:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((ditaa . t)
   (tcl . t)
   (ruby . t)
   (sqlite . t)
   (clojure . t)
   (lisp . t)
   (http . t)
   (sql . t)))


Now, in the org file itself, place this kind of block:
#+BEGIN_SRC sqlite :db pieces-and-providers.sqlite
#+END_SRC

Now, when pressing c-c c-c inside a block, a new block called RESULTS will be created underneath with the result of the eval'ed block. 

 There are many other options on exporting org-blocks and other options for fine tunning how results are shown, or formatted. For me, this is a nice way to have my experiments documented, or leave exercises for my future me to solve. For example, I copied most of the sql exercises wikibook and created this repo with the stories/exercises as org files so I can try them in an interactive way.

No hay comentarios: