On Jul 23, 2006, at 5:47 PM, Jamey Cribbs wrote:

> I think the following code is an example of what John is pointing out:
>
> class Employee
>  def self.find(&block)
>    instance_eval(&block)
>  end
> end
>
> class Department
>  def initialize
>    @dept = 'Accounting'
>  end
>
>  def employees
>    Employee.find { puts "employee.dept == #{@dept}" }
>  end
> end
>
> Department.new.employees
>
> Run this and you get:
>
>    employee.dept ==
>
> Replace that instance_eval in Employee.find with a yield statement,  
> run it again, and you get:
>
>    employee.dept == Accounting

Hmm. Right.

Okay, though this is getting perverse...

module Mongoose
   def find(klass, &block)
     klass.mongoose_find(self, block)
   end

   module ClassMethods
     def mongoose_find(obj, block)
       obj.instance_eval(&block)
     end
     def check_find(block)
       instance_eval(&block)
     end
   end

   def self.included(klass);
     klass.extend(ClassMethods);
   end
end

class Employee
   include Mongoose
end

class Department
   include Mongoose

   def initialize
     @dept = 'Accounting'
   end

   def employees
     find(Employee) { puts "FIND:  employee.dept == #{@dept}" }
   end
end

Department.new.employees


I don't know why I'm arguing this because I actually like the  
Employee.find { |emp| emp.dept == @dept } syntax better anyway.

Cheers,
Bob

----
Bob Hutchison                  -- blogs at <http://www.recursive.ca/ 
hutch/>
Recursive Design Inc.          -- <http://www.recursive.ca/>
Raconteur                      -- <http://www.raconteur.info/>
xampl for Ruby                 -- <http://rubyforge.org/projects/xampl/>