Created
February 16, 2014 12:41
-
-
Save carpodaster/9033643 to your computer and use it in GitHub Desktop.
Difficulties accessing an anonymous class' name in its superclass' self.inherited
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
require 'minitest/autorun' | |
class BaseBuilder | |
class << self | |
attr_accessor :klass | |
end | |
def self.inherited(subclass) | |
super | |
subclass.klass = (subclass.name || subclass.to_s).gsub(/Builder\z/, '') | |
end | |
end | |
class ObjectBuilder < BaseBuilder | |
end | |
describe 'Accesses the class name in self.inherited' do | |
it 'cuts off "Builder" and returns "Object"' do | |
ObjectBuilder.name.wont_be_nil | |
ObjectBuilder.klass.must_equal 'Object' | |
end | |
it 'has problems with anonymous classes' do | |
Class.new(BaseBuilder) do | |
def self.to_s | |
'FooBuilder' | |
end | |
end.klass.must_equal 'Foo' | |
end | |
it 'does not have a name either' do | |
StringBuilder = Class.new(BaseBuilder) | |
StringBuilder.klass.must_equal 'String' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment