On Sat, 20 Aug 2005, Andrew S. Townley wrote:

> Hmmm... interesting approach.  I'm not sure I quite need the indirection
> you've illustrated, but it's interesting to see all the different ways you
> can solve the problem in Ruby.  The passing the class declaration as a block
> to the Class:new method will require me to do some more reading.  I haven't
> looked into the reflection/class operations much yet.  I want to make sure
> that I understand exactly what's happening here.  Instinctively, I think I
> understand what's going on, but need to double-check.  Some things are still
> Ruby-101 to me at this stage.

i agree it's overkill - but i does prevent the classes from being introduced
as valid constants when the file is required and it allows you you basically
have the classes themselves as private instance variables.  this is about as
protected as you can get in ruby - since there's always instance_eval...
nevertheless it does afford total control over how these objects get created.

> Also, I have a question further down...
>
> On Fri, 2005-08-19 at 16:20, Ara.T.Howard wrote:
>>      harp:~ > cat a.rb
>>      module Factory
>>        @classes = {
>>          'a' => (
>>              Class::new do
>>                class << self
>>                  def foo; 42; end
>>                end
>>                def bar; 42.0; end
>>              end
>>          ),
>>          'b' => (
>>              Class::new do
>>                class << self
>>                  def foo; 'FORTY-TWO'; end
>>                end
>>                def bar; 'forty-two'; end
>>              end
>>          ),
>>        }
>>        def self::new(*a, &b)
>>          class_for(a.shift)::new(*a, &b)
>
> I'm assuming you're doing the 'a.shift' call here to allow you to specify
> all of the arguments you might need to the constructor.  Is that correct?
> Thus you could potentially write something like:
>
> 	ref = Factory::new 'a', 'param1', 'param2'
>
> which would allow you to have the class 'a' support a constructor taking the
> two parameters (and, of course, the optional block).

exactly - in reality you'd probably determine which class to do based on the
args alone, or something else...

cheers.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze.  --Nagarjuna
===============================================================================