[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

find opened classes

nraychaudhuri@gmail.com

8/22/2007 1:56:00 AM

Is there any way I can find all the classes that have been re-opened
in a project?

4 Answers

Logan Capaldo

8/22/2007 2:08:00 AM

0

On 8/21/07, nraychaudhuri@gmail.com <nraychaudhuri@gmail.com> wrote:
> Is there any way I can find all the classes that have been re-opened
> in a project?
>
>
>

No fool proof way. Are there particular changes to a class you are
looking to track? For instance you can override method_added to see if
anyone adds any additional methods.

Joel VanderWerf

8/22/2007 2:19:00 AM

0

nraychaudhuri@gmail.com wrote:
> Is there any way I can find all the classes that have been re-opened
> in a project?
>

If you don't mind having a trace proc running while you classes are
being loaded you can do this. (You can always set_trace_func(nil) later
on if you want to stop wasting cpu cycles.)

opened = {}

class_tracer = proc do |event, file, line, id, binding, classname|
case event
when "class" # class or module definition
cl = eval("self", binding)
opened[cl] = true
when "c-call"
case id
when :class_eval, :module_eval
cl = eval("self", binding)
opened[cl] = true
end
end
end

set_trace_func(class_tracer)

class Foo
end
class String
end
Array.class_eval {}

p opened.keys # ==> [Foo, Array, String]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Klemme

8/22/2007 6:23:00 AM

0

2007/8/22, Joel VanderWerf <vjoel@path.berkeley.edu>:
> nraychaudhuri@gmail.com wrote:
> > Is there any way I can find all the classes that have been re-opened
> > in a project?
> >
>
> If you don't mind having a trace proc running while you classes are
> being loaded you can do this. (You can always set_trace_func(nil) later
> on if you want to stop wasting cpu cycles.)
>
> opened = {}
>
> class_tracer = proc do |event, file, line, id, binding, classname|
> case event
> when "class" # class or module definition
> cl = eval("self", binding)
> opened[cl] = true
> when "c-call"
> case id
> when :class_eval, :module_eval
> cl = eval("self", binding)
> opened[cl] = true
> end
> end
> end
>
> set_trace_func(class_tracer)
>
> class Foo
> end
> class String
> end
> Array.class_eval {}
>
> p opened.keys # ==> [Foo, Array, String]

I thought of something similar but I would just dump out class names
as they come (probably to $stderr). That way you can see when classes
are opened multiple times. Also, if you include all the info you get
during tracing you can easily locate the code:

RKlemme@padrklemme1 /cygdrive/c/Archives/jboss.org/jboss-4.2.1.GA-src
$ ruby -e 'set_trace_func lambda {|*a| p a if a.first == "class"};
class Foo; end'
["class", "-e", 1, nil, #<Binding:0x1002fdb8>, false]

Kind regards

robert

nraychaudhuri@gmail.com

8/23/2007 12:42:00 PM

0

Thanks a lot