[#1026] Is this a bug? — Dave Thomas <Dave@...>

18 messages 2000/01/03

[#1084] Infinite loop — Dave Thomas <Dave@...>

17 messages 2000/01/11

[#1104] The value of while... — Dave Thomas <Dave@...>

24 messages 2000/01/11

[ruby-talk:01024] Tracing attributes

From: Dave Thomas <Dave@...>
Date: 2000-01-03 05:19:28 UTC
List: ruby-talk #1024
I've been playing with a tracing module. One of the things it does is
define a tracing version of 'attr'

     class Module
       def trace_attr(id, writable=false) 
         name = id.id2name
         class_eval %{
           def #{name}
             trace "Accessing #{name} (", @#{name}, ")\n"
             @#{name}
           end
          }
          if (writable)
            class_eval %{
             def #{name}=(val)
               trace "#{name} (was ", @#{name}, ") => ", val, "\n"
               @#{name} = val
              end
            }
          end     
       end
     end

So you can log accesses to an attribute by defining it using 'trace_attr':

     class Test
       trace_attr :fred, true
       //...
     end

Two questions:

1. Is this a reasonable thing to do? Is it OK to mess around with
   methods in Module?

2. Is this an acceptable implementation? 

Thanks

Dave




In This Thread

Prev Next