Hi --

On Wed, 15 Aug 2007, Douglas F Shearer wrote:

> On 14 Aug 2007, at 22:24, jantzeno wrote:
>> 
>> Is it possible to instantiate a class using a string from the array so
>
> names.each do |n|
> 	eval n + ' = Person.new'
> end

That won't work, both because it's inside a block:

   1.times do
     david = 1
   end

   p david   # error -- undefined

and because eval creates its own binding for local variable
assignments:

   eval("a = 1")
   p a

For both reasons, the variable would already have to be in view before
the eval.

The best and most common advice given in response to this question is:
do it with a hash instead, like this:

   people = {}
   names.each {|name| people[name] = Person.new }


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)