Giles Bowkett wrote:

> 
> The gateway link to this list is the top link on ruby-forum.com.
> Considering how easy it would be for a newbie to just click the first
> link on the list, the newbie questions we get are actually pretty
> good. But I e-mailed the Ruby Forum guy anyway to see if I could
> persuade him to move the link down the list a bit and maybe put the
> Rails list link at the top. No dice so far. No response so far, in
> fact.
> 

Yep, i'm getting to this list via ruby-forum.com... However i did 
specifically post in the ruby list as opposed to the rails one, since it 
did seem like a ruby-specific question... i mean, yes, i _am_ trying to 
populate a database via rails, in case you're wondering :)... but it's 
the ruby part that i'm having trouble with.

> 
> Also:
> 
>> here's the thing: i've got a couple of arrays with several items in
>> each. I'd like to use a block to iterate through each array, and create
>> objects of the class that is named the same as the array.
> 
> I would say that the real way to fix this would be to fix this part
> here. That's really where the bug is. Naming the array after the
> class. If you have that data, you can also use it to create a string,
> and that's so much easier.
> 
> Instead of
> 
> arbitrary_class = [item, other_item]
> 
> And then a whole bunch of "meta" stuff, including an eval on the class
> name - which you have to create an array of strings to do anyway - I
> would recommend maybe this:
> 
> classes_with_items = {"ArbitraryClass" => ["item", "other_item"]}
> 
> and then
> 
> classes_with_items.each do |class_name, array|
>   array.each do |item_name|
>     class_name.constantize.new(:item_name => item_name)
>   end
> end
> 
> It's a lot easier to read and you won't be cursing yourself a month
> later when you find the code, read it, and need to remember what it
> does and how it does it.
> 

So, you're basically using a hash to map Class names to their 
corresponding arrays? That's pretty cool!!! :) kinda makes everything 
much easier.... a super elegant solution, actually. I didn't realize you 
could have an array inside of a hash (why not, now that i think of 
it)...
I'll try it this way, thanks! (with .constantize as well as with 
.const_get, just for the sake of Ruby purity)...
-- 
Posted via http://www.ruby-forum.com/.