some fun with ruby lambdas

written by sam on August 26th, 2007 @ 03:44 PM

introducing SourceProc

In Ruby, some objects cannot be dumped with Marshal, including procedure or methods, bindings, continuations, IO instances or singleton objects.

I personnaly needed to serialize some procedures once, so I decided to create a class of procedures that know their source code and can be marshalled.

Here is SourceProc, read the documentation for more information.

YAGNI, just for fun ;)

a simple example

1
2
3
4
5
6
7
8
9
10
11
12
13
14

  add = slambda '|x, y| x + y'

  add.call(10, Math::PI)
  # => 13.1415926535898

  add.source
  # => '|x, y| x + y'
  
  add.to_s      
  # => 'slambda("|x, y| x + y")'

  Marshal.load(Marshal.dump(add)).call(2, 3)
  # => 5

Comments

  • bioskop on 28 Aug 09:16

    Hey, that's pretty cool :) Very nice and usefull !

Post a comment

Options:

Size

Colors