From: Jason Roelofs Date: 2007-06-05T05:53:04+09:00 Subject: Re: Help needed: Dynamically create a Proc from a String and evaluate it ------=_Part_9998_28854483.1180990373976 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Don't you mean g.call(1.0, 2, 5) ? You have to do proc.call, they aren't regular methods. Jason On 6/4/07, Axel Etzold wrote: > > 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 =3D> "eval('proc{|c,x,k| c*Math.exp(x/k) }')" > g=3Deval(f)=3D> # > > p g(1.0).call(2,5) =3D> 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=3D'f(x)=3Dc*exp(x/k)', > > > > If you're passing in c and k each time, then > > > > b =3D 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 =3D 5 > > k =3D 2 > > a =3D 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 =3D 5 > > k =3D 2 > > a =3D eval("proc { |x| c*Math.exp(x/k) }") > > puts a.call(3) > > > > Brian. > > -- > Psssst! Schon vom neuen GMX MultiMessenger geh=F6rt? > Der kanns mit allen: http://www.gmx.net/de/go/multimessenger > > ------=_Part_9998_28854483.1180990373976--