From: Axel Etzold Date: 2007-06-05T05:44:46+09:00 Subject: Re: Help needed: Dynamically create a Proc from a String and evaluate it Dear Brian, thank you for responding. However, I still haven't solved my problem. So, following one of your suggestions, I get: p 'function-text' p f => "eval('proc{|c,x,k| c*Math.exp(x/k) }')" g=eval(f)=> # p g(1.0).call(2,5) => undefined function or variable 'g' I can't get to use the proc I just created.. Why not ? Thank you, Axel -------- Original-Nachricht -------- Datum: Tue, 5 Jun 2007 04:55:57 +0900 Von: Brian Candler An: ruby-talk@ruby-lang.org Betreff: Re: Help needed: Dynamically create a Proc from a String and evaluate it > On Tue, Jun 05, 2007 at 04:18:58AM +0900, Axel Etzold wrote: > > I need help in dynamically creating a Proc from a String like, say, > > > > a='f(x)=c*exp(x/k)', > > If you're passing in c and k each time, then > > b = eval("proc{|c,x,k| c*Math.exp(x/k) }") > puts b.call(5.0, 2.0, 1.0) > > This means you only call eval once, to create the proc object, then you > can > call it as many times as you like. > > If c and k really are constants then you can substitute them in to the > proc > definition: > > c = 5 > k = 2 > a = eval("proc { |x| #{c}*Math.exp(x/#{k}) }") > puts a.call(3) > > or you can use the closure property to bind the local variables directly: > > c = 5 > k = 2 > a = eval("proc { |x| c*Math.exp(x/k) }") > puts a.call(3) > > Brian. -- Psssst! Schon vom neuen GMX MultiMessenger geh�rt? Der kanns mit allen: http://www.gmx.net/de/go/multimessenger