On Wed, 29 Mar 2006, John Carter wrote:

> Some old Pascal implementations had (and I think some still do) had a
> facility to "mark" the heap, and then at some point "release" all
> items allocated after that mark.
>
> Here is a nifty way of doing the same (and more!) in ruby....
>
> ==========================try.rb======================================
> pid = Process.fork do
>  # Load any modules we need
>  require 'find'
>
>  a = 'x' * 100*1024*1024
>
>
> end
>
> pid, result = Process.waitpid2( pid)
> ======================================================================

you can get an object back from the child using:

     harp:~ > cat a.rb
     def child
       r, w = IO.pipe
       IO.popen('-') do |pipe|
         if pipe
           w.close
           buf = pipe.read
           pipe.close
           raise Marshal.load(r.read) unless $? == 0
           Marshal.load(buf)
         else
           r.close
           begin
             print(Marshal.dump(yield))
           rescue Exception => e
             w.print(Marshal.dump(e))
             exit! 42
           end
         end
       end
     ensure
       r.close
     end

     emsg = lambda{|e| STDERR.puts %Q[#{ e.message } (#{ e.class })\n#{ e.backtrace.join "\n" }]}

     p child{ 'value from child' } rescue emsg[$!]

     p child{ error_from_child } rescue emsg[$!]

     p 'but the parent lives'



     harp:~ > ruby a.rb
     "value from child"
     undefined local variable or method `error_from_child' for main:Object (NameError)
     a.rb:29
     a.rb:14:in `child'
     a.rb:4:in `child'
     a.rb:29
     "but the parent lives"


with all the same memory preserving side effects.

regards.

-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama