Kent Sibilev
6/27/2005 6:23:00 AM
I might be wrong here, but I think you proc method is not faster or
it's even slower than plain eval method. Every time you call a proc,
eval gets called as well.
You can try to define your proc like so:
p = eval "lambda{#{c}}"
Kent.
On 6/27/05, Neville Burnell <Neville.Burnell@bmsoft.com.au> wrote:
> Hi,
>
> I have an active record collection and I need to 'eval' a string against
> each row from the collection. Instead of processing the 'eval' for each
> row, I was thinking to 'cache' the eval into a Proc to for reuse, ie:
>
> irb(main):001:0> @a = 1
> => 1
> irb(main):002:0> @b = 7
> => 7
> irb(main):003:0> c = %q{ @a + @b }
> => " @a + @b "
> irb(main):004:0> d = Proc.new { eval c }
> => #<Proc:0x02ad1240@(irb):4>
> irb(main):005:0> d.call
> => 8
> irb(main):006:0> @a = 2
> => 2
> irb(main):007:0> d.call
> => 9
> irb(main):008:0>
>
> Is my assumption that eval is relatively expensive, and that a Proc call
> is fast correct?
> Any suggestions on how to do this better?
>
> Thanks
>
> Nev
>
>