questions about Class#remove_class implementation
I am reading Rails's Class#remove_class:
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.send :remove_const, basename unless parent == klass
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
irb(main):002:0> klass = (class << ActiveRecord::Base; self; end)
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?
PS: Rails defines Module#parent as
parent_name = name.split('::')[0..-2] * '::'
parent_name.empty? ? Object : parent_name.constantize
|All times are GMT. The time now is 04:33 PM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.