Federico Brubacher wrote:
>> That's one way to do it.  Actually only a smaller change is needed
>> because the root cause in this case is the scoping rule of the
>> iteration variable in a "for" loop which is different than when
>> iterating with a block as this example demonstrates:
>>
> 
> CORRECT :
> 
>>     names.each do |name|
>>       define_method("m1_#{name}") do
>>         puts "1_#{name}"
>>       end
>>     end
> 
> INCORRECT !
> 
> 
>>     for name in names do
>>       define_method("m2_#{name}") do
>>         puts "2_#{name}"
>>       end
>>     end

I'd still point out that using both of the above code, the name variable
becomes part of the defined methods' default binding (which in the first
case is shared among the methods, in the second case are per-method).
This means that if you happen to alter the variable, it'll keep its new
value for subsequent calls to the methods.

If you use the eval way, then the define-time value of the variable gets
evaluated, the value itself becomes part of the method code, and there
remain no local variables in the methods' default binding.

Someone correct me if I'm wrong.

mortee