Next: , Up: Tutorial   [Contents]


2.1 Creating and Running Individual Blocks

Blocks can be created using the make-block* function. Useful blocks need the code and language keyword arguments.

(use-modules (blocks)
             (blocks language ruby))

(define my-block (make-block*
                  #:language ruby
                  #:code "
def fibonacci(n)
   n <= 1 ? n :  fibonacci(n - 1) + fibonacci(n - 2)
end

5.times do |n|
  puts \"#{n} #{fibonacci(n)}\"
end"))

Blocks are run using the run-block function:

(run-block my-block)
;; => $1 = #<block... result: "0 0\n1 1\n2 1\n3 2\n4 3\n"...>

This returns a new block, largely identical to my-block but with the result field set. my-block was not modified; if my-block is run again the Ruby subprocesses will be reinvoked.

To get the result, use the block-result function.

(block-result (run-block my-block))
;; => $1 = "0 0\n1 1\n2 1\n3 2\n4 3\n"

As a shorthand, pass #:run? #t to make-block* to simultaneously create and run a block.