Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Created February 16, 2014 12:41
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 carpodaster/9033643 to your computer and use it in GitHub Desktop.
Save carpodaster/9033643 to your computer and use it in GitHub Desktop.
Difficulties accessing an anonymous class' name in its superclass' self.inherited
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