From: Mathieu Bouchard Date: 2001-10-06T13:12:45+09:00 Subject: [ruby-talk:22150] Re: Marshal won't dump a Proc On Thu, 4 Oct 2001, HarryO wrote: > In article , "Mathieu > Bouchard" wrote: > If I understand what you're getting at, the extension you've made to the > kernel module allows me to do either of these ... > p1 = proc { puts "hello, world" } > p2 = proc %{ puts "hello, world" } > Is that right? yes. > Assuming you've read my long-winded reply, explaining what I'm trying > to achieve, I could do something like ... > fibonacci = Stream.new([1, 1]) %{ |s, i| s[i - 2] + s[i - 1] } On Thu, 4 Oct 2001, HarryO wrote: > fibonacci = Stream.new([1, 1]) proc %{ |s, i| s[i - 2] + s[i - 1] } neither is correct. The trick only works with proc. but you may use this identity: foo(a,b,c) { d } is functionally equivalent to: foo a,b,c,&proc { d } oh, and by the way, if you need to access outer-local scopes, you can't use the percent trick: it won't pick up the correct scope (not even the correct self). You'd need the "binding.caller" feature, which is in a RCR that I submitted some time ago; as a substitute using current means, you'd have to write: foo a,b,c,proc binding, %{ d } in Ruby 1.6, or: foo a,b,c,proc(binding, %{ d }) in Ruby 1.7 (which is slightly more cumbersome). Alternately you could get away with: foo a,b,c,proc(%{ d }){} where you use the empty proc as a binding (iirc). ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju