Created
August 14, 2009 21:30
-
-
Save thinkerbot/168116 to your computer and use it in GitHub Desktop.
Danger of accessing constant name during inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This illustrates the danger of accessing the constant name | |
# of a child class during inheritance (ex by to_s). The | |
# constant name will not be set at the time of inheritance | |
# if the class was is using Class.new. | |
class A | |
class << self | |
attr_accessor :const_name | |
def inherited(child) | |
child.const_name = child.to_s | |
end | |
end | |
end | |
class B < A; end | |
puts B.const_name # => B | |
C = Class.new(A) | |
puts C.const_name # => #<Class:...> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment