From: Chris Hall Date: 2008-02-15T03:57:21+09:00 Subject: Re: lambda/procs and dynamically generated methods Adam Shelly wrote: > On 2/14/08, Chris Hall wrote: >> @action.call >> end >> end >> > > I may be missing something important about what you are trying to > accomplish, but > what about simply > > class Job > def initialize(file = nil, &block) > raise "need file or block" if file.nil? && !block_given? > @action = block || proc { eval IO.read(file)} > end > def do > @action.call > end > end > > (I sure hope you trust the contents of 'file') > -Adam The main reason the code is the way it is, is because I don't want to used named blocks, see: http://ola-bini.blogspot.com/2007/12/ruby-closures-and-memory-usage.html The only really difference between the 2 is that in once instance I am capturing the block in @action and in the other I am creating a method from the block. I didn't know if one way was more efficient than the other. And yes, I will be trusting the code in file. :) I'm not necessarily looking for the simplest solution, I'm looking for the best solution. Both of the examples I posted have the same results, it's just how they are implemented. I didn't know there was some behind the scenes mo-jo going on that makes one a better choice than the other. -- Posted via http://www.ruby-forum.com/.