# Find opened classes

**URL:** <https://www.ruby-forum.com/t/find-opened-classes/112686>\
**Category:** Ruby\
**Created:** [August 22, 2007, 4:01am UTC](https://www.ruby-forum.com/t/find-opened-classes/112686 "2007-08-22T04:01:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 4:01am UTC](https://www.ruby-forum.com/t/find-opened-classes/112686/1 "2007-08-22T04:01:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 4:09am UTC](https://www.ruby-forum.com/t/find-opened-classes/112686/2 "2007-08-22T04:09:24Z")

</div>

On 8/21/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 4:19am UTC](https://www.ruby-forum.com/t/find-opened-classes/112686/3 "2007-08-22T04:19:25Z")

</div>

removed\_email\_address@domain.invalid 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]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 8:24am UTC](https://www.ruby-forum.com/t/find-opened-classes/112686/4 "2007-08-22T08:24:39Z")

</div>

2007/8/22, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> end
> 
> ```
> 
> 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
