From: Ge Bro Date: 2007-11-25T05:04:22+09:00 Subject: Re: Iterating through class names using a block Ge Bro wrote: > Great, so with everyone's help this is what I ended up doing in the end: > > foo = [1foo, 2foo, 3foo] > bar = [abar, bbar] > > ['foo', 'bar'].each do |this_array| > length = 0 > this_obj = this_array.to_s.capitalize.constantize > until length == eval(this_array).length > this_obj.create(:word => eval(this_array)[length]) > length += 1 > end > > the trick was to use the .constantize method to turn 'foo' and 'bar' > into classes Foo and Bar, and then to use eval() to use the array's > names instead of its contents. > > I came across .constantize after searching for const_get as phrogz > suggested. > > 7stud, my classes are defined in their corresponding controllers - this > is a part of a Rails exercise > > dusty, your method using collect and compact looks interesting, I'll > play with it too. > > Thanks everyone for help. I haven't been excited about programming for > the last 15 years, but Ruby is changing that rapidly :) btw, i do have to admit that i have no clear idea of why eval() works here and what it's actually supposed to do. Just reading about it now and trying to make sense of it. -- Posted via http://www.ruby-forum.com/.