<p>Joe Van Dyk <a href="http://www.zenspider.com/pipermail/ruby/2008-August/004223.html" rel="noreferrer">asked the Ruby mailing list</a>:</p>

<blockquote>
  <p>Hi,</p>
  
  <p>In Ruby, I guess you can't marshal a lambda/proc object, right?  Is
  that possible in lisp or other languages?</p>
  
  <p>What I was trying to do:</p>
</blockquote>

<pre><code>l = lamda { ... }
Bj.submit "/path/to/ruby/program", :stdin =&gt; Marshal.dump(l)
</code></pre>

<blockquote>
  <p>So, I'm sending BackgroundJob a lambda object, which contains the
  context/code for what to do.  But, guess that wasn't possible.  I
  ended up marshaling a normal ruby object that contained instructions
  for what to do after the program ran.</p>
  
  <p>Joe</p>
</blockquote>

[{'answer_id': 23974, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p>Try <a href="http://seattlerb.rubyforge.org/ruby2ruby/" rel="nofollow noreferrer">ruby2ruby</a></p>\n'}, {'answer_id': 38588, 'author': 'ryantm', 'author_id': 823, 'author_profile': 'https://Stackoverflow.com/users/823', 'pm_score': 5, 'selected': True, 'text': '<p>You cannot marshal a Lambda or Proc. This is because both of them are considered closures, which means they close around the memory on which they were defined and can reference it. (In order to marshal them you\'d have to Marshal all of the memory they could access at the time they were created.)</p>\n\n<p>As Gaius pointed out though, you can use <a href="https://github.com/seattlerb/ruby2ruby" rel="nofollow noreferrer">ruby2ruby</a> to get a hold of the string of the program. That is, you can marshal the string that represents the ruby code and then reevaluate it later. </p>\n'}, {'answer_id': 2376645, 'author': 'Jonathan Tran', 'author_id': 12887, 'author_profile': 'https://Stackoverflow.com/users/12887', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re interested in getting a string version of Ruby code using Ruby2Ruby, you might like <a href="https://stackoverflow.com/questions/199603/how-do-you-stringize-serialize-ruby-code">this thread</a>.</p>\n'}, {'answer_id': 3705317, 'author': 'dominic', 'author_id': 122875, 'author_profile': 'https://Stackoverflow.com/users/122875', 'pm_score': 4, 'selected': False, 'text': '<p>you could also just enter your code as a string:</p>\n\n<pre><code>code = %{\n    lambda {"hello ruby code".split(" ").each{|e| puts e + "!"}}\n}\n</code></pre>\n\n<p>then execute it with eval </p>\n\n<pre><code>eval code\n</code></pre>\n\n<p>which will return a ruby lamda.</p>\n\n<p>using the <code>%{}</code> format escapes a string, but only closes on an unmatched brace. i.e. you can nest braces like this <code>%{ [] {} }</code> and it\'s still enclosed. </p>\n\n<p>most text syntax highlighters don\'t realize this is a string, so still display regular code highlighting.</p>\n'}, {'answer_id': 33620677, 'author': 'Peter P.', 'author_id': 1490262, 'author_profile': 'https://Stackoverflow.com/users/1490262', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve found proc_to_ast to do the best job: <a href="https://github.com/joker1007/proc_to_ast" rel="nofollow">https://github.com/joker1007/proc_to_ast</a>.</p>\n\n<p>Works for sure in ruby 2+, and I\'ve created a PR for ruby 1.9.3+ compatibility(<a href="https://github.com/joker1007/proc_to_ast/pull/3" rel="nofollow">https://github.com/joker1007/proc_to_ast/pull/3</a>)</p>\n'}, {'answer_id': 43352463, 'author': 'Shimaa Marzouk', 'author_id': 1863857, 'author_profile': 'https://Stackoverflow.com/users/1863857', 'pm_score': 0, 'selected': False, 'text': '<p>If proc is defined into a file, U can get the file location of proc then serialize it, then after deserialize use the location to get back to the proc again</p>\n\n<blockquote>\n  <p>proc_location_array = proc.source_location</p>\n</blockquote>\n\n<p>after deserialize:</p>\n\n<blockquote>\n  <p>file_name = proc_location_array[0]</p>\n  \n  <p>line_number = proc_location_array[1]</p>\n  \n  <p>proc_line_code = IO.readlines(file_name)[line_number - 1]</p>\n  \n  <p>proc_hash_string = proc_line_code[proc_line_code.index("{")..proc_line_code.length]</p>\n  \n  <p>proc = eval("lambda #{proc_hash_string}")</p>\n</blockquote>\n'}, {'answer_id': 52353048, 'author': 'Paul Brannan', 'author_id': 244083, 'author_profile': 'https://Stackoverflow.com/users/244083', 'pm_score': 1, 'selected': False, 'text': '<p>Once upon a time, this was possible using ruby-internal gem (<a href="https://github.com/cout/ruby-internal" rel="nofollow noreferrer">https://github.com/cout/ruby-internal</a>), e.g.:</p>\n\n<pre><code>p = proc { 1 + 1 }    #=&gt; #&lt;Proc&gt;\ns = Marshal.dump(p)   #=&gt; #&lt;String&gt;\nu = Marshal.load(s)   #=&gt; #&lt;UnboundProc&gt;\np2 = u.bind(binding)  #=&gt; #&lt;Proc&gt;\np2.call()             #=&gt; 2\n</code></pre>\n\n<p>There are some caveats, but it has been many years and I cannot remember the details.  As an example, I\'m not sure what happens if a variable is a dynvar in the binding where it is dumped and a local in the binding where it is re-bound.  Serializing an AST (on MRI) or bytecode (on YARV) is non-trivial.</p>\n\n<p>The above code works on YARV (up to 1.9.3) and MRI (up to 1.8.7).  There\'s no reason why it cannot be made to work on Ruby 2.x, with a small amount of effort.</p>\n'}]
['https://Stackoverflow.com/questions/23970', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']