From: chr_news@... (chr_news@...) Date: 2001-12-06T13:52:12+09:00 Subject: [ruby-talk:27610] Procs and evaling and functional composition Hi, this is a follow up on an old RCR from Robert Feld on functional composition of proc's (method objects?). I came up with a very simple pure Ruby implementation but run into an interesting(?) problem of evaling Proc objects. The algorithm is as follows: class Proc def *(other) # check arity's and raise an exceptions if they are # out side a given range lets say -17...17. (Table[other.arity+17])[self,other] end Table = Array.new 35 # populate Table via #eval with Proc object's of type lambda {|g,f| \ lambda {|x_1,x_2,x_3,| g[*(f.to_a(x_1,_x_2,x_3))] } } # The main difference to the original RCR is the use # of *(f.to_a(x_1,_x_2,x_3)) over the plain f(x_1,x_2,x_3) # as an argument to be feed into g. end The problem with this approach is that the call of (Table[other.arity+17])[self,other] remembers the #eval context (i.e. eval is called) - since the Proc object Table[other.arity+17] was brought into live within the eval scope. In the particular instance the work around is to explicitly write out (generate) these 35 Proc's but it leaves me wandering if it is possible to dynamically generate a Proc via eval within the current scope? Also what is the general mood on adding functional composition to Proc's interface? /Christoph