In article <4249B4AA.2090602 / alumni.rice.edu>,
B. K. Oxley (binkley) <binkley / alumni.rice.edu> wrote:
>Florian Gross wrote:
>> Glenn Smith wrote:
>>> class Meta
>>>   def self.has_many( sym )
>>>     attr_tmp = "def  #{sym.to_s} \n"
>>>     attr_tmp << " @#{sym.to_s} = [] unless @#{sym.to_s}\n"
>>>     attr_tmp << " @#{sym.to_s}; end"
>>>     eval ( attr_tmp  )
>>>   end
>>> end
>> 
>> class Meta
>>   def self.has_many(sym)
>>     ivar = :"@#{sym}"
>>     define_method(sym.to_sym) do
>>       new_value = instance_variable_get(ivar) || []
>>       instance_variable_set(ivar, new_value)
>>       new_value
>>     end
>>   end
>> end
>
>What is the difference between these two approaches?
>
>* Is one more 'culturally correct'?
>* Does one perform better?
>* Is one more future-proof?

The second approach would generally be considered 'better'.  eval must be 
used with care (security issues, debugging is harder, etc.).  The second 
approach doesn't use eval so that's why it would generally be preferred.  
Performance differences between the two are probably negligable.  I 
suspect that either approach would work in the future.  

Sometimes you need to use eval (or it's cousins instance_eval and 
class_eval - the latter should probably be preferred where possible) and 
it's great to have it's power available so I'm not saying you should 
_never_ use eval.

Phil