Robert Klemme
4/27/2009 6:14:00 AM
On 26.04.2009 21:55, Rick DeNatale wrote:
> On Sun, Apr 26, 2009 at 3:30 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> On 26.04.2009 19:40, Tim Conner wrote:
>
>>> My code currently looks like this:
>>> class BatchExternalBooking
>>> def method_missing(method_sym, *args)
>>> if method_sym.to_s =~ /^message_thread_([0-9]*)=?(\w*)?$/
>>> BatchExternalBooking.instance_eval "attr_accessor
>>> :message_thread_#{$1}"
>> You might rather want to use class_eval here.
>
> No instance_eval works just as well for sending attr_accessor to a
> class/module. The only difference with class_eval is when using def to
> define a method.
I did not want to state that instance_eval is the issue. Sorry for
being imprecise. A simple
BatchExternalBooking.send "attr_accessor", "message_thread_#$1"
would be sufficient.
>> Also, as Robert pointed out already, using $1 only once and storing the
>> value in a local variable is safer because it can be changed by any method
>> you invoke.
>>
>>> self.send("message_thread_#{$1}=", $2)
>
> That's not true either. The $n variables are frame local, in a given
> invocation frame they will only change when another regex match is
> done in the same invocation.
A simple test verifies this to be true. But now I wonder how I have
come to this misconception. I am pretty sure I stored $1 in a local
variable to avoid issues with changing values. Maybe I just had another
match in the same method and extended the overwriting problem to method
calls.
Thank you for the education, Rick!
Kind regards
robert