Hi --

On Mon, 21 Nov 2005, Damphyr wrote:

> I'm too lazy to figure it out (and a bit hangover).
> I have several collections of objects (read over an OLE API) that I transform 
> to arrays of ruby objects.
> Against the DRY religion I ended up with several methods similar to the 
> following:
>
> def get_processors col
> 	ar=Array.new
> 	count=col.Count

Are you sure you've got Count as a method name?  It's possible, but
fairly bizarre.

> 	1.upto(count){|i|
> 		ar<<Processor.new(col.GetAt(i))
> 	}
> 	return ar
> end
>
> Now, this begs for the working equivalent of
>
> def get_collection col, _class
> 	ar=Array.new
> 	count=col.Count
> 	1.upto(count){|i|
> 		ar<<_class.new(col.GetAt(i))
> 	}
> 	return ar
> end

I guess col is 1-originating, so you'd have to adapt this, but here it
is with a normal array:

   def get_collection(col, klass)
     col.map {|e| klass.new(e) }
   end

If klass is a string, you'd have to do a const_get on it first:

   klass = Object.const_get(klass)

or the traditional deep version, if it's a nested constant:

   class Module
     def deep_const_get(c)
       c.split("::").inject(self) {|acc,const| acc.const_get(const) }
     end
   end

or some variant thereof.


David

-- 
David A. Black
dblack / wobblini.net