X
Xavier Noria
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:
irb(main):002:0> klass = (class << ActiveRecord::Base; self; end)
=> #<Class:ActiveRecord::Base>
irb(main):003:0> klass.name
=> ""
irb(main):004:0> klass.to_s.split("::").last
=> "Base>"
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
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:
irb(main):002:0> klass = (class << ActiveRecord::Base; self; end)
=> #<Class:ActiveRecord::Base>
irb(main):003:0> klass.name
=> ""
irb(main):004:0> klass.to_s.split("::").last
=> "Base>"
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