Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created August 14, 2009 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkerbot/168116 to your computer and use it in GitHub Desktop.
Save thinkerbot/168116 to your computer and use it in GitHub Desktop.
Danger of accessing constant name during inheritance
# 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