# Questions about Class#remove\_class implementation

**URL:** <https://www.ruby-forum.com/t/questions-about-class-remove-class-implementation/75569>\
**Category:** Ruby\
**Created:** [November 11, 2006, 1:53am UTC](https://www.ruby-forum.com/t/questions-about-class-remove-class-implementation/75569 "2006-11-11T01:53:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 11, 2006, 1:53am UTC](https://www.ruby-forum.com/t/questions-about-class-remove-class-implementation/75569/1 "2006-11-11T01:53:59Z")

</div>

I am reading Rails’s Class#remove\_class:

def remove\_class(\*klasses)  
klasses.flatten.each do |klass|  
# Skip this class if there is nothing bound to this name  
next unless defined?(klass.name)

```
   basename = klass.to_s.split("::").last
   parent = klass.parent

   # Skip this class if it does not match the current one bound

```

to this name  
next unless parent.const\_defined?(basename) && klass =  
parent.const\_get(basename)

```
   parent.send :remove_const, basename unless parent == klass
 end

```

end

and have a few questions:

1. Do you think there’s a reason for using Module#name  
first and Module#to\_s afterwards? Looks to me that  
they are only different for singleton classes, and  
for those ones split does not provide a meaningful  
chunk anyway:

2. In which situation do you think parent.const\_defined?(basename)  
&& klass = parent.const\_get(basename) could return false?

3. Does parent.send :remove\_const effectively unload the  
class definition from the interpreter? If yes, what  
would happen to some dangling instance afterwards if  
some method of that class was invoked? Would the object  
still hold some kind of relationship to the now non-  
existent class that could result in some fatal error?

4. When could parent == klass hold at the end of the block?

– fxn

PS: Rails defines Module#parent as

def parent  
parent\_name = name.split(’::’)[0…-2] \* ‘::’  
parent\_name.empty? ? Object : parent\_name.constantize  
end
